I am making a script for a game system and when the table is empty I want the game to end. But for some reason it doesn’t work.
local alivePlrs = {}
while true do
wait(5)
for i, plr in pairs(game.Players:GetPlayers()) do
plr.Character.HumanoidRootPart.CFrame = CFrame.new(11.2, 0.5, 52.62)
table.insert(alivePlrs, plr)
print(alivePlrs)
-- Story Starts Here
plr.Character.Humanoid.Died:Connect(function()
table.remove(alivePlrs, alivePlrs[plr])
print(alivePlrs)
if (next(alivePlrs) == nil) then -- This too
workspace.Events.EndGame:Fire()
end
end)
end
wait(100)
game.Players.PlayerRemoving:Connect(function(plr)
print(plr.Name)
table.remove(alivePlrs, alivePlrs[plr])
print(alivePlrs)
wait(0.5)
if (next(alivePlrs) == nil) then -- This part here
workspace.Events.EndGame:Fire()
end
end)
end
workspace.Events.EndGame.Event:Connect(function()
print("Game Over")
workspace["Ahh, fresh meat!"]:Play()
alivePlrs = {}
return
end)
You’re never skipping to the next iteration of your round loop when the table is empty. Hint: you can do this easily using continue, which skips back to the top of the loop.
function isEmpty()
for i, v in alivePlrs do
if #alivePlrs == nil then
workspace.Events.EndGame:Fire()
end
return false
end
return true
end
while true do
wait(5)
for i, plr in pairs(game.Players:GetPlayers()) do
plr.Character.HumanoidRootPart.CFrame = CFrame.new(11.2, 0.5, 52.62)
table.insert(alivePlrs, plr)
print(alivePlrs)
plr.Character.Humanoid.Died:Connect(function()
table.remove(alivePlrs, alivePlrs[plr])
print(alivePlrs)
isEmpty()
end)
end
end
like that, idk if im doing it wrong the exact thing you put just gives an error
isEmpty() returns a boolean if the table is empty, it doesn’t bind to any game logic. You should do this
if isEmpty(alivePlrs) then
--logic if there are no more alive players
end
Also you modified my function
So essentially you would want this instead
function isEmpty(t)
for i, v in t do
return false
end
return true
end
while true do
wait(5)
for i, plr in pairs(game.Players:GetPlayers()) do
plr.Character.HumanoidRootPart.CFrame = CFrame.new(11.2, 0.5, 52.62)
table.insert(alivePlrs, plr)
print(alivePlrs)
plr.Character.Humanoid.Died:Connect(function()
table.remove(alivePlrs, alivePlrs[plr])
if isEmpty(alivePlrs) then
workspace.Events.EndGame:Fire()
end
end)
end
end