Menu
CoddyTech

Deux cartes

Leçon 15 sur 18 du cours Jeu Roblox : Survivre aux catastrophes de Coddy.

ServerStorage contient une deuxième carte, Rooftops : un carré de briques plus petit avec quatre grands piliers. Chaque manche peut charger l’une ou l’autre. GetChildren() répertorie les cartes, et une position aléatoire en sélectionne une ; FindFirstChild(name) en trouve une par son nom, pour Settings.Map.

local function pickMap()
	local maps = ServerStorage.Maps
	if Settings.Map then
		return maps:FindFirstChild(Settings.Map)
	end
	local all = maps:GetChildren()
	return all[math.random(1, #all)]
end

Les deux cartes suivent le même plan, avec un Base et un Spawn au même endroit, afin que chaque catastrophe et la téléportation fonctionnent sur l’une comme sur l’autre. Une nouvelle carte n’a qu’à suivre ce plan pour rejoindre le jeu.

Les joueurs aiment savoir où ils vont : le tableau indique le nom de la carte, et la catastrophe attend deux secondes pendant qu’ils regardent autour d’eux.

challenge icon

Défi

Difficile

Charger une carte aléatoire :

  1. Écrivez pickMap() comme dans la leçon.
  2. Pendant la manche, utilisez local template = pickMap(), clonez-la comme avant et affichez Map: Island (avec le Name du modèle) sur le tableau.
  3. Après la téléportation des joueurs, attendez avec task.wait(2) avant la catastrophe.

Appuyez sur Play pendant quelques manches pour voir les deux cartes, puis sur Check.

Essayez vous-même

local Players = game:GetService("Players")
local ServerStorage = game:GetService("ServerStorage")
local TweenService = game:GetService("TweenService")
local Debris = game:GetService("Debris")
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 d’état 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

local function defeat(hit)
	local humanoid = hit.Parent:FindFirstChild("Humanoid")
	if humanoid then
		humanoid.Health = 0
	end
end

-- Une étiquette flottante "Survivor" au-dessus de la tête d’un joueur
local function tagSurvivor(character)
	local tag = Instance.new("BillboardGui")
	tag.Name = "SurvivorTag"
	tag.Size = UDim2.fromOffset(120, 30)
	tag.StudsOffset = Vector3.new(0, 2.5, 0)
	tag.Parent = character.Head

	local label = Instance.new("TextLabel")
	label.Size = UDim2.fromScale(1, 1)
	label.BackgroundTransparency = 1
	label.Text = "Survivor"
	label.TextColor3 = Color3.fromRGB(80, 255, 120)
	label.TextScaled = true
	label.Parent = tag
end

-- Les catastrophes, classées par nom
local Disasters = {}

function Disasters.Flood(map)
	local base = map.Base
	local water = Instance.new("Part")
	water.Name = "Water"
	water.Anchored = true
	water.CanCollide = false
	water.Transparency = 0.3
	water.Color = Color3.fromRGB(40, 120, 255)
	water.Size = Vector3.new(base.Size.X + 10, 1, base.Size.Z + 10)
	water.Position = base.Position - Vector3.new(0, 2, 0)
	water.Parent = map
	water.Touched:Connect(defeat)

	local rise = TweenInfo.new(Settings.RoundLength * 0.8, Enum.EasingStyle.Linear)
	TweenService:Create(water, rise, { Position = water.Position + Vector3.new(0, 8, 0) }):Play()
end

function Disasters.Meteors(map)
	local base = map.Base
	task.spawn(function()
		while map.Parent do
			local meteor = Instance.new("Part")
			meteor.Name = "Meteor"
			meteor.Shape = Enum.PartType.Ball
			meteor.Size = Vector3.new(3, 3, 3)
			meteor.Color = Color3.fromRGB(255, 120, 30)
			meteor.Material = Enum.Material.Neon
			local x = base.Position.X + math.random(-15, 15)
			local z = base.Position.Z + math.random(-15, 15)
			meteor.Position = Vector3.new(x, 40, z)
			meteor.Parent = map
			meteor.Touched:Connect(defeat)
			Debris:AddItem(meteor, 4)
			task.wait(0.5)
		end
	end)
end

function Disasters.Shrink(map)
	local base = map.Base
	local shrink = TweenInfo.new(Settings.RoundLength * 0.7, Enum.EasingStyle.Linear)
	TweenService:Create(base, shrink, { Size = Vector3.new(6, base.Size.Y, 6) }):Play()
end

local function pickDisaster()
	if Settings.Disaster then
		return Settings.Disaster
	end
	local names = {}
	for name in Disasters do
		table.insert(names, name)
	end
	table.sort(names)
	return names[math.random(1, #names)]
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)
			tagSurvivor(character)
			survivors[player] = true
			character.Humanoid.Died:Connect(function()
				survivors[player] = nil
			end)
		end
	end

	local disaster = pickDisaster()
	Disasters[disaster](map)
	for seconds = Settings.RoundLength, 1, -1 do
		setStatus(disaster .. "! Time left: " .. seconds)
		task.wait(1)
	end

	local names = {}
	for player in survivors do
		player.leaderstats.Wins.Value += 1
		local tag = player.Character and player.Character.Head:FindFirstChild("SurvivorTag")
		if tag then
			tag:Destroy()
		end
		teleport(player, lobbySpawn)
		table.insert(names, player.Name)
	end
	map:Destroy()
	if #names > 0 then
		setStatus("Winners: " .. table.concat(names, ", "))
	else
		setStatus("Nobody survived!")
	end
	task.wait(3)
end
quiz iconTestez-vous

Cette leçon comprend un petit quiz. Commencez la leçon pour y répondre et suivre votre progression.

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