생존자는 누구일까
Coddy의 Roblox 게임: 재난 생존 코스 레슨. 18개 중 7번째.
누가 이겼는지 알려면 게임은 누가 아직 살아 있는지 알아야 합니다. 플레이어를 키로 사용하는 테이블은 집합처럼 작동합니다. survivors[player] = true는 플레이어를 추가하고, survivors[player] = nil은 플레이어를 제거하며, for player in survivors do는 남아 있는 플레이어를 모두 방문합니다.
Humanoid는 체력이 0에 도달하면 Died를 발생시킵니다. 라운드가 시작될 때 이에 연결하면 플레이어가 쓰러지는 순간 집합에서 제외됩니다.
survivors[player] = true
character.Humanoid.Died:Connect(function()
survivors[player] = nil
end)라운드가 시작될 때 살아 있는 플레이어만 라운드에 참여합니다. 부활을 기다리는 플레이어는 Health가 0인 죽은 캐릭터를 가지고 있으며, 그 캐릭터를 순간 이동시키면 죽은 플레이어를 생존자로 계산하게 됩니다.
챌린지
중급각 라운드의 생존자를 추적하세요:
- 라운드가 시작되면 빈 테이블
survivors를 만드세요. - 캐릭터가 존재하고
Humanoid.Health가 0보다 큰 플레이어만 순간 이동시키세요. 각 플레이어를survivors에 추가하고, Humanoid가 죽으면 제거하세요. - 라운드가 끝나면 생존자만 로비로 순간 이동시키고, 생존자가 몇 명이었는지 출력하세요:
Survivors: 1.
Play를 누른 다음, 두 번째 라운드에서 섬 밖으로 걸어 나가 Output을 확인하세요. 그런 다음 Check를 누르세요.
직접 해보기
local Players = game:GetService("Players")
local ServerStorage = game:GetService("ServerStorage")
local Settings = require(game.ReplicatedStorage.Settings)
local lobbySpawn = workspace.LobbySpawn
print("Rounds last " .. Settings.RoundLength .. " seconds")
-- 모든 플레이어에게 리더보드의 Wins가 주어집니다
Players.PlayerAdded:Connect(function(player)
local leaderstats = Instance.new("Folder")
leaderstats.Name = "leaderstats"
leaderstats.Parent = player
local wins = Instance.new("IntValue")
wins.Name = "Wins"
wins.Parent = leaderstats
end)
-- 로비의 상태 표시판
local gui = Instance.new("SurfaceGui")
gui.Parent = workspace.StatusBoard
local status = Instance.new("TextLabel")
status.Name = "Status"
status.Size = UDim2.fromScale(1, 1)
status.BackgroundColor3 = Color3.fromRGB(25, 25, 35)
status.TextColor3 = Color3.fromRGB(255, 255, 255)
status.TextScaled = true
status.Text = "Welcome to Disaster Survival!"
status.Parent = gui
local function setStatus(text)
status.Text = text
end
local function teleport(player, target)
local character = player.Character
if character then
character:PivotTo(target.CFrame + Vector3.new(0, 4, 0))
end
end
-- 라운드 반복문
while true do
for seconds = Settings.Intermission, 1, -1 do
setStatus("Intermission: " .. seconds)
task.wait(1)
end
local map = ServerStorage.Maps.Island:Clone()
map.Name = "Map"
map.Parent = workspace
for _, player in Players:GetPlayers() do
teleport(player, map.Spawn)
end
for seconds = Settings.RoundLength, 1, -1 do
setStatus("Time left: " .. seconds)
task.wait(1)
end
for _, player in Players:GetPlayers() do
teleport(player, lobbySpawn)
end
map:Destroy()
end
이 레슨에는 짧은 퀴즈가 포함되어 있습니다. 레슨을 시작해 문제를 풀고 진행 상황을 기록하세요.