Menu
CoddyTech

מי בחיים

שיעור 7 מתוך 20 בקורס משחק Roblox: הישרדות באסונות של Coddy.

כדי לדעת מי ניצח, המשחק צריך לדעת מי עדיין בחיים. טבלה שמפתחותיה הם שחקנים פועלת כמו קבוצה: survivors[player] = true מוסיף שחקן, survivors[player] = nil מסיר שחקן, ו-for player in survivors do עובר על כל מי שנשאר.

אובייקט Humanoid מפעיל את Died כשהבריאות שלו מגיעה ל-0. חיבור אליו כשהסבב מתחיל מוציא שחקן מהקבוצה ברגע שהוא נופל:

survivors[player] = true
character.Humanoid.Died:Connect(function()
	survivors[player] = nil
end)

רק שחקנים שחיים כשהסבב מתחיל מצטרפים אליו: למי שממתין להופיע מחדש יש דמות מתה עם Health בערך 0, ושיגור שלה יחשיב שחקן מת כשורד.

challenge icon

אתגר

בינוני

עקוב אחר מי שעדיין בחיים בסבב.

  1. כשהסבב מתחיל, צור local survivors = {}.
  2. טלפורט רק שחקנים שהדמות שלהם כוללת Humanoid.Health שערכו מעל 0, והוסף כל אחד מהם אל survivors.
  3. חבר את Humanoid.Died של כל אחד אל survivors[player] = nil.
  4. כשהסבב מסתיים, טלפורט בחזרה רק את השורדים, ואז הדפס "Survivors: " .. count.

לחץ על "שחק": כשהסבב מסתיים, הפלט מציג Survivors: 1. ואז לחץ על "בדיקה".

נסו בעצמכם

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
		-- שיעור 7: כאן יופיע הקוד של Who Is Alive
		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

כל השיעורים ביחידה משחק Roblox: הישרדות באסונות

תרגלו בעצמכם: Roblox אונליין