Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
COMMUNITY QUESTION

Why wont the map delete and round restart when all players di?

So when every player dies on the map the map is supposed to delete and the round restart. but when every player dies it doesnt

local run = true
local Message = workspace.LobbyMessage
local start = true
while wait() do
	local AlivePlayers = 0
wait(4)
if start then
	start = false
Message.Text = "Preparing..." -- we'll get fancy with GUIs later
local maps = game.ReplicatedStorage.Maps:GetChildren()
Map = maps[math.random(1,#maps)]
local NewMap = Map:Clone()
NewMap.Parent = workspace
wait(1)
Message.Text = ""
for i,players in pairs(game.Players:GetPlayers()) do
	AlivePlayers  = game.Players.NumPlayers
	players.Character.Humanoid.Died:connect(function()
	AlivePlayers = AlivePlayers - 1	
	end)
players.Character.Torso.CFrame = CFrame.new(NewMap.Start.Position.X+i,NewMap.Start.Position.Y+12,NewMap.Start.Position.Z+i)
players.Character.Humanoid.WalkSpeed = 0
wait(2)
Message.Text = "GO"
players.Character.Humanoid.WalkSpeed = 50
wait(.3)
Message.Text = ""
end
if AlivePlayers == 0 then
			run = false
		Message.Text = "Everyone died... gg"
		wait(3)
		Message.Text = ""
		NewMap:Destroy()
		end
NewMap.End.Touched:connect(function(It)
	if run == true then
	start = true
local spawn = workspace.SpawnLocation
local player = game.Players:GetPlayerFromCharacter(It.Parent)
	print("waiting")
for i,players in pairs(game.Players:GetPlayers()) do
players.Character.Torso.CFrame = CFrame.new(spawn.Position.X+i,spawn.Position.Y+12,spawn.Position.Z+i)
workspace.LobbyMessage.Text = (player.Name.." Has won")
player.leaderstats.Coins.Value = player.leaderstats.Coins.Value+50
NewMap:Destroy()
wait(3)
workspace.LobbyMessage.Text = ("")
wait(5)
end
end
end)
end
end

2 answers

You're not continuously checking if all the players have died, so the if statement from line 29 to 35 won't execute at all if AlivePlayers is anything but 0. What we need to do is repeatedly check if AlivePlayers is under or equal to 0 (we do this so that if the game glitches at all, the game will reset without errors.)

To do this, you could use a repeat loop to wait until AlivePlayers is under or equal to 0 and use the built in spawn function so that the yielding from the repeat loop is done in a separate thread so other code can execute after it.


Code:

spawn(function() --Spawn lets us create a new thread, so the code after this function will run without waiting for AlivePlayers to be under or equal to 0.
	repeat wait() until AlivePlayers <= 0 --Waits until AlivePlayers is under or equal to 0.
	run = false
	Message.Text = "Everyone died... gg"
	wait(3)
	Message.Text = ""
	NewMap:Destroy()
end)

I hope my answer helped you. If it did, be sure to accept it.

This discussion is preserved as it was. New answers, comments, and votes are closed.