Menu
CoddyTech

Gagnants

Leçon 8 sur 20 du cours Jeu Roblox : Survivre aux catastrophes de Coddy.

Survivre à une manche rapporte une victoire. À la fin de la manche, chaque joueur encore dans survivors en gagne une :

for player in survivors do
	player.leaderstats.Wins.Value += 1
end

+= ajoute une valeur directement. Pour savoir si quelqu’un a survécu, next(survivors) renvoie la première clé de la table, ou nil si elle est vide.

Les joueurs ont besoin d’un moment pour lire le résultat avant que le compte à rebours du lobby ne reprenne le tableau ; la manche se termine donc par task.wait(3).

challenge icon

Défi

Moyen

Accorde une victoire à chaque survivant.

  1. À la fin de la manche, ajoute 1 à Wins de chaque survivant au lieu de les compter.
  2. Après la destruction de la carte, affiche Survivors win! si quelqu’un a survécu, sinon Nobody survived!
  3. Ensuite, exécute task.wait(3) avant le prochain compte à rebours dans le hall.

Appuie sur « Jouer » et survis à une manche : Wins passe à 1 et le tableau affiche Survivors win. Puis appuie sur « Vérifier ».

Essayez vous-même

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")

-- Chaque joueur reçoit des Wins dans le classement
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)

-- Le panneau de statut dans le hall
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

-- La boucle des manches
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

	local survivors = {}
	for _, player in Players:GetPlayers() do
		local character = player.Character
		if character and character.Humanoid.Health > 0 then
			teleport(player, map.Spawn)
			survivors[player] = true
			character.Humanoid.Died:Connect(function()
				survivors[player] = nil
			end)
		end
	end

	for seconds = Settings.RoundLength, 1, -1 do
		setStatus("Time left: " .. seconds)
		task.wait(1)
	end

	local count = 0
	for player in survivors do
		count += 1
		teleport(player, lobbySpawn)
	end
	map:Destroy()
	-- Leçon 8 : Winners va ici
	print("Survivors: " .. count)
end

Toutes les leçons de Jeu Roblox : Survivre aux catastrophes

Entraînez-vous par vous-même : Playground Roblox