Hey there, I’m trying to make a round system for one of my games that I’m working on, and for some reason, it’s not working.
Error: So, you may notice in the code I put several print statements to print out the playersInRound table. However, what’s happening that is weird, is that at first it prints out the entire table perfectly fine over and over.
But, right when the second-to-last player dies, where it’s supposed to end the game and announce the winner, that last print statement outside of the while loop prints that the table is empty.
As you see here, it prints the table fine, then right as the second to last player dies, it prints an empty table.
local status = game.ReplicatedStorage.Status
local replStorage = game.ReplicatedStorage
function startGame()
local playersInGame = game.Players:GetPlayers()
for i, player in pairs(game.Players:GetPlayers()) do
local char = player.Character
if char then
local humanoidRootPart = char.HumanoidRootPart
humanoidRootPart.CFrame = game.Workspace:FindFirstChild("TpToPart").CFrame
end
end
while #playersInGame > 1 do
task.wait()
print(playersInGame)
for i, v in pairs(playersInGame) do
v.Character.Humanoid.Died:Connect(function()
local index = table.find(playersInGame, v)
table.remove(playersInGame, index)
end)
end
print(playersInGame)
game.Players.PlayerRemoving:Connect(function(plr)
local index = table.find(playersInGame, plr)
if not index then
else
table.remove(playersInGame, index)
end
end)
status.Value = "Game in progress..."
print(playersInGame)
replStorage.DestroyPart.OnServerEvent:Connect(function(player, item)
item:Destroy()
end)
print(playersInGame)
end
print(playersInGame)
local winner = playersInGame[1]
status.Value = "Winner: "..winner.Name
winner.leaderstats.Wins.Value += 1
winner:LoadCharacter()
game.Workspace:FindFirstChild("Map"):Destroy()
local clone = replStorage.Map:Clone()
clone.Parent = workspace
task.wait(5)
end
while true do
for i = 10, 0, -1 do
status.Value = "Game starting in: "..i
task.wait(1)
end
startGame()
end