Menu
CoddyTech

그것이 걷는다

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

Shadow는 가만히 서 있습니다. Shadow의 생각은 반복문입니다. 0.25초마다 가장 가까이에 있는 살아 있는 플레이어를 향해 걸어갑니다. 반복문은 끝나지 않으므로 task.spawn을 사용해 자체 스레드에서 실행하고, addRoom은 동시에 계속 진행됩니다.

local function runMonster(shadow)
	while shadow.Parent do
		-- 누군가를 찾아 그쪽으로 걸어간다
		task.wait(0.25)
	end
end

task.spawn(runMonster, monster)

while shadow.Parent는 몬스터가 파괴되면 반복문을 중지합니다.

challenge icon

챌린지

중급

Shadow가 너를 향해 걸어오게 하세요.

  1. nearestCharacter(position)를 작성하세요. 가장 가까이 있는 살아 있는 플레이어의 캐릭터와 그 거리를 반환합니다.
  2. runMonster(shadow)를 작성하세요. shadow.Parent인 동안 가장 가까운 캐릭터의 HumanoidRootPart.Position으로 MoveTo한 다음 task.wait(0.25)를 실행합니다.
  3. spawnMonster의 끝에 task.spawn(runMonster, monster)를 추가하세요.

‘플레이’를 눌러 3번 방까지 가 보세요. Shadow가 곧장 너를 향해 옵니다. 그런 다음 ‘확인’을 누르세요.

직접 해보기

local Players = game:GetService("Players")
local ServerStorage = game:GetService("ServerStorage")
local Lighting = game:GetService("Lighting")
local TweenService = game:GetService("TweenService")
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

-- 레슨 10: 걷는 녀석이 여기에 들어간다

-- 괴물이 뒤에서 나타난다: 가장 최근에 생성된 방보다 두 방 앞선 방의 입구에서
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
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)

-- 가장 최근에 생성된 방의 출구에 새 방을 만든다
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
	-- 가장 최근 방과 그 뒤의 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

-- 모든 플레이어: 리더보드에 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 플레이그라운드