Menu
CoddyTech

붙잡힘

Coddy의 Roblox 게임: 어둠 속 탈출 코스 레슨. 25개 중 14번째.

지금까지 몬스터는 당신에게 다가와 그 자리에 서 있었습니다. Settings.CatchDistance 안에 들어오면 몬스터가 당신을 잡습니다. Humanoid의 체력이 0이 되고, 당신은 체크포인트로 돌아옵니다.

그러면 몬스터는 사라지고, 다음 문이 열릴 때 다시 나타나 당신의 뒤에 옵니다. 따라서 몬스터는 더 이상 MonsterFrom 방에서만 생성되는 것이 아니라, 몬스터가 없을 때마다 그 방부터 이후의 어느 방에서든 생성됩니다.

if count >= Settings.MonsterFrom and not monster then
	spawnMonster()
end
challenge icon

챌린지

중급

그림자가 당신을 붙잡습니다.

  1. runMonster에서 이동하기 전에, 가장 가까운 캐릭터가 Settings.CatchDistance 이내에 있으면 해당 캐릭터의 Humanoid의 Health를 0으로 설정하고, 그림자를 제거한 다음 monster = nil로 설정하고 반환하세요.
  2. addRoom에서 count >= Settings.MonsterFrom이고 not monster이면 몬스터를 생성하세요.

‘플레이’를 누르고 몬스터가 당신에게 닿게 하세요. 체크포인트에서 다시 시작합니다. 그런 다음 ‘확인’을 누르세요.

직접 해보기

local Players = game:GetService("Players")
local ServerStorage = game:GetService("ServerStorage")
local Lighting = game:GetService("Lighting")
local TweenService = game:GetService("TweenService")
local PathfindingService = game:GetService("PathfindingService")
local Settings = require(game.ReplicatedStorage.Settings)

local rooms = workspace.Rooms
local lastRoom = rooms.Lobby -- 가장 최근에 생성된 방
local count = 0 -- 지금까지 생성된 방의 수
local monster = nil

-- 밤이 찾아온다: 태양은 없고, 곳곳에 희미한 빛과 안개가 있다
Lighting.ClockTime = Settings.ClockTime
Lighting.Ambient = Settings.Ambient
Lighting.OutdoorAmbient = Settings.Ambient
Lighting.FogColor = Color3.new(0, 0, 0)
Lighting.FogEnd = Settings.FogEnd
print("Lights out")

-- 플레이어가 소지한 도구: 손에 들고 있거나 Backpack에 있는 것
local function findTool(player, name)
	return (player.Character and player.Character:FindFirstChild(name)) or player.Backpack:FindFirstChild(name)
end

-- 경첩을 축으로 문을 돌려 연다
local function openDoor(door)
	door:SetAttribute("Open", true)
	door.Panel.Prompt.Enabled = false
	local hinge, panel = door.Hinge, door.Panel
	local goal = hinge.CFrame * CFrame.Angles(0, math.rad(90), 0) * hinge.CFrame:ToObjectSpace(panel.CFrame)
	TweenService:Create(panel, TweenInfo.new(0.6), { CFrame = goal }):Play()
end

-- MonsterSight 범위 안에서 잡을 수 있는 가장 가까운 살아 있는 플레이어(숨지 않은 플레이어)와 그 거리
local function nearestCharacter(position)
	local best, bestDistance = nil, Settings.MonsterSight
	for _, player in Players:GetPlayers() do
		local character = player.Character
		local root = character and character:FindFirstChild("HumanoidRootPart")
		if root and character.Humanoid.Health > 0 and not character:GetAttribute("Hidden") then
			local distance = (root.Position - position).Magnitude
			if distance < bestDistance then
				best, bestDistance = character, distance
			end
		end
	end
	return best, bestDistance
end

-- 목표 지점을 향해 이동한다: 길이 막히지 않으면 직선으로, 아니면 경로를 따라
local function walkToward(shadow, goal, path)
	local root = shadow.HumanoidRootPart
	local params = RaycastParams.new()
	params.FilterDescendantsInstances = { shadow }
	local hit = workspace:Raycast(root.Position, goal - root.Position, params)
	if not hit or hit.Instance.Parent:FindFirstChildOfClass("Humanoid") then
		shadow.Humanoid:MoveTo(goal)
		return
	end
	path:ComputeAsync(root.Position, goal)
	local points = path:GetWaypoints()
	if path.Status == Enum.PathStatus.Success and points[2] then
		shadow.Humanoid:MoveTo(points[2].Position)
	end
end

-- 괴물의 행동 방식: 0.25초마다 누군가를 쫓는다
local function runMonster(shadow)
	local humanoid = shadow.Humanoid
	local root = shadow.HumanoidRootPart
	local path = PathfindingService:CreatePath()
	while shadow.Parent do
		local character = nearestCharacter(root.Position)
		-- Lesson 14: Caught는 여기에 넣는다
		if character then
			walkToward(shadow, character.HumanoidRootPart.Position, path)
		else
			humanoid:MoveTo(root.Position)
		end
		task.wait(0.25)
	end
end

-- 괴물이 뒤에서 나타난다: 가장 최근 방보다 두 방 앞선 방의 입구에서
local function spawnMonster()
	local behind = nil
	for _, room in rooms:GetChildren() do
		if room:GetAttribute("Number") == count - 2 then
			behind = room
		end
	end
	if not behind then
		return
	end
	monster = ServerStorage.Shadow:Clone()
	monster:PivotTo(behind.Entrance.CFrame + Vector3.new(0, 3.1, 0))
	monster.Humanoid.WalkSpeed = Settings.MonsterSpeed
	monster.Parent = workspace
	task.spawn(runMonster, monster)
end

local addRoom -- 아래에서 정의됨: 문을 열면 방이 추가되고, 방에는 문이 있다

-- 플레이어가 문을 열 때 일어나는 일
local function setupDoor(door)
	door.Panel.Prompt.Triggered:Connect(function(player)
		if door:GetAttribute("Open") then
			return
		end
		if door:GetAttribute("Locked") then
			local key = findTool(player, "Key")
			if not key then
				return
			end
			key:Destroy()
			door:SetAttribute("Locked", nil)
		end
		if door.Parent == lastRoom then
			addRoom()
		end
		openDoor(door)
	end)
end
setupDoor(workspace.Rooms.Lobby.Door)

-- 방에 들어가면 그 방이 플레이어의 Room이자 체크포인트가 됩니다
local function setupEntry(room)
	room.Entry.Touched:Connect(function(hit)
		local player = Players:GetPlayerFromCharacter(hit.Parent)
		if player then
			local number = room:GetAttribute("Number")
			player.leaderstats.Room.Value = number
			player.RespawnLocation = room.Checkpoint
		end
	end)
end
setupEntry(workspace.Rooms.Lobby)

-- 옷장: E를 눌러 숨고, 다시 E를 눌러 나옵니다
local function setupCloset(closet)
	closet.Front.Prompt.Triggered:Connect(function(player)
		local character = player.Character
		if character:GetAttribute("Hidden") then
			character:PivotTo(closet.Front.CFrame * CFrame.new(-3, -1, 0) * CFrame.Angles(0, math.rad(90), 0))
			character:SetAttribute("Hidden", nil)
		else
			character:PivotTo(closet.Inside.CFrame + Vector3.new(0, 2.5, 0))
			character:SetAttribute("Hidden", true)
		end
	end)
end

-- 가장 최근 방의 출구에 새 방을 만듭니다
function addRoom()
	count += 1
	local name = Settings.Order[(count - 1) % #Settings.Order + 1]
	if count >= Settings.RoomsToEscape then
		name = "Exit"
	end
	local room = ServerStorage.Rooms[name]:Clone()
	room:PivotTo(lastRoom.Exit.CFrame)
	room:SetAttribute("Number", count)
	room.Sign.SurfaceGui.Number.Text = "Room " .. count
	room.Parent = rooms
	lastRoom = room
	setupDoor(room.Door)
	setupEntry(room)
	-- 잠긴 방은 열쇠를 방 안에 보관합니다
	local keySpot = room:FindFirstChild("KeySpot")
	if keySpot then
		local key = ServerStorage.Key:Clone()
		key.Handle.CFrame = keySpot.CFrame
		key.Parent = room
		key.Handle.Prompt.Triggered:Connect(function(player)
			key.Handle.Prompt.Enabled = false
			key.Parent = player.Backpack
		end)
	end
	local closet = room:FindFirstChild("Closet")
	if closet then
		setupCloset(closet)
	end
	-- 가장 최근 방과 그 뒤의 KeepRooms개 방을 유지합니다
	for _, old in rooms:GetChildren() do
		local number = old:GetAttribute("Number")
		if number < count - Settings.KeepRooms then
			print("Room " .. number .. " is gone")
			old:Destroy()
		end
	end
	if count == Settings.MonsterFrom then
		spawnMonster()
	end
	return room
end

-- 옷장에서 나오면 더 이상 숨어 있지 않습니다
task.spawn(function()
	while true do
		task.wait(0.25)
		for _, player in Players:GetPlayers() do
			local character = player.Character
			if character and character:GetAttribute("Hidden") then
				local closest = math.huge
				for _, inside in workspace.Rooms:GetDescendants() do
					if inside.Name == "Inside" then
						closest = math.min(closest, (inside.Position - character.HumanoidRootPart.Position).Magnitude)
					end
				end
				if closest > 4 then
					character:SetAttribute("Hidden", nil)
				end
			end
		end
	end
end)

-- 모든 플레이어: 리더보드에 Room을 표시하고, 매번 부활할 때 손전등을 지급합니다
Players.PlayerAdded:Connect(function(player)
	local leaderstats = Instance.new("Folder")
	leaderstats.Name = "leaderstats"
	leaderstats.Parent = player

	local room = Instance.new("IntValue")
	room.Name = "Room"
	room.Parent = leaderstats

	player.CharacterAdded:Connect(function()
		local flashlight = ServerStorage.Flashlight:Clone()
		flashlight.Parent = player.Backpack
		local beam = flashlight.Handle.Beam
		flashlight.Activated:Connect(function()
			beam.Enabled = not beam.Enabled
		end)
	end)
end)

Roblox 게임: 어둠 속 탈출의 모든 레슨

직접 연습해 보세요: Roblox 플레이그라운드