Basically, a part of my script (lines 44-85) isn't working. I want it to check if all players died in the game, but all it does it continue after everyone dies. I don't know why, because it checks if "dead >= #game:GetService("Players").NumPlayers", but it still won't work. Help?
Error: ServerScriptService.Main:54: attempt to compare number with nil
local serverStorage = game:GetService("ServerStorage") local maps = serverStorage:WaitForChild("Maps"):GetChildren() local statusText = serverStorage:WaitForChild("statusText") while wait(1) do for intermissionTime = 10, 1, -1 do statusText.Value = intermissionTime .. " until a new round starts!" wait(1) end statusText.Value = "Choosing a map..." wait(3) repeat chosenMap = maps[math.random(1, #maps)] until chosenMap.Name ~= oldMap statusText.Value = "The chosen map is " .. chosenMap.Name .. " by " .. chosenMap.Creator.Value .. "!" local chosen = chosenMap:Clone() chosen.Parent = workspace wait(3) for waitTime = 5, 1, -1 do statusText.Value = "Teleporting players in... " .. waitTime wait(1) end for i, v in pairs(game:GetService("Players"):GetPlayers()) do local spawns = chosen:FindFirstChild("Spawns"):GetChildren() v.won.Value = false v.Character.Humanoid.WalkSpeed = 0 v.Character:MoveTo(spawns[i].Position) end wait(3) statusText.Value = "All players have 60 seconds to finish. Go!" for i, v in pairs(game:GetService("Players"):GetPlayers()) do v.Character.Humanoid.WalkSpeed = 16 end winner = "" dead = 0 allplayersDead = false local players = game.Players:GetPlayers() for _, player in pairs(players) do local living = Instance.new("BoolValue", player.PlayerGui) living.Name = "Living" end while wait(1) and roundTime > 0 and allplayersDead == false do for _, player in pairs(players) do if player ~= nil then if player.PlayerGui:FindFirstChild("Living") == nil then dead = dead + 1 end if dead >= #players then allplayersDead = true return {} end end end dead = 0 statusText.Value = roundTime .. " seconds remain!" roundTime = roundTime - 1 end repeat wait() until roundTime == 0 or winner ~= "" or allplayersDead == true playerWin = nil oldMap = chosenMap.Name chosenMap:Destroy() if winner ~= "" then statusText.Value = winner.Name .. " has won!" winner.leaderstats.Points.Value = winner.leaderstats.Points.Value + 10 wait(3) elseif allplayersDead then statusText.Value = "Everyone has died..." wait(3) elseif roundTime == 0 then statusText.Value = "The round time is up!" wait(3) end end
The script at the end, along with being quite ineffecient, is too complicated. As BlueTaslem said, there can be a better way with a simplier script.
Normally i'd take out most of the variables you have here, but i kept in the most possible because it seems you prefer ineffeciency.
Anyway, a very simple way to get the players playing the game is to parent their characters elsewhere where you can separate them from the non-playing players. You will have to parent them back afterwards.
(I'd perhaps make this more effecient now that you told me you prefer anything and that this way was the only way you knew, but SH wouldn't help me.)
--Previous lines excluded for obvious reasons winner = "" dead = 0 allplayersDead = false roundTime = 60 local players = game.Players:GetPlayers() for _, player in pairs(players) do --This is meant to be done once when all players enter local living = Instance.new("BoolValue", player.PlayerGui) living.Name = "Living" end while wait(1) and roundTime > 0 and allplayersDead == false do for _, player in pairs(players) do if player ~= nil then --If the player leaves during game if player.PlayerGui:FindFirstChild("Living") == nil then dead = dead + 1 end if dead >= #players then allplayersDead = true return {} end end end dead = 0 statusText.Value = roundTime .. " seconds remain!" roundTime = roundTime - 1 end repeat wait() until roundTime == 0 or winner ~= "" or allplayersDead == true playerWin = nil oldMap = chosenMap.Name chosenMap:Destroy() if winner ~= "" then statusText.Value = winner.Name .. " has won!" winner.leaderstats.Points.Value = winner.leaderstats.Points.Value + 10 wait(3) elseif allplayersDead then statusText.Value = "Everyone has died..." wait(3) elseif roundTime == 0 then statusText.Value = "The round time is up!" wait(3) end end