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

1 comment
Haven't looked through this completely yet, but at line 52, you should be checking if the character even has a humanoid, just to be safe. If they don't it will return nil without an output