Spectate Gui is removing the wrong player from a Table

So basically I’m making a game spectate gui and everytime a new game starts the table gets players added to a table and if a player dies he gets removed from the table but the thing is when i tested it the wrong player gets removed from the table and idk how to fix it.

heres the code

local plrs = {}

StartEvent.OnClientEvent:Connect(function(plr)

table.insert(plrs, plr)

end)

contestantDeath.OnClientEvent:Connect(function(plr)

table.remove(plrs, plrs[plr])

end)

game.Players.PlayerRemoving:Connect(function(plr)

if localPlayer:FindFirstChild(“Contestant”) then

table.remove(plrs, plrs[plr])

end

end)

1 Like

The table.remove() function’s second parameter is the position of the table where you want to remove something. You can use table.find() to get the position of the player in the table.

local plrs = {}
StartEvent.OnClientEvent:Connect(function(plr)
    table.insert(plrs, plr)
end)
contestantDeath.OnClientEvent:Connect(function(plr)
    table.remove(plrs, table.find(plrs, plr))
end)
game.Players.PlayerRemoving:Connect(function(plr)
    if localPlayer:FindFirstChild("Contestant") then
        table.remove(plrs, table.find(plrs, plr))
    end
end)
1 Like