Hello so what I'm trying to do here is on line 32 I'm removing the player UserId From the table but when the players is removed (Which is not getting removed) it's showing after 3 seconds in the output that there's still 2 players and the "Winner" is Player1 but Player2 is still in the map and Player1 is in the lobby, and the table named "playeringame" is saying the the first value of the table is Player1. And also on line 26 I'm adding the player to the "playeringame"
Here's the code:
local playeringame = {}
local Status = game.ReplicatedStorage:WaitForChild("Status")
Status.Value = "Waiting For Players..."
game.Players.PlayerAdded:Connect(function(player)
repeat
wait()
until #game:GetService("Players"):GetPlayers() >= 1
wait(2)
for i = 4, 1, -1 do
Status.Value = "Staring Game in "..i
wait(1)
end
Status.Value = "Loading Map..."
local ClonedMap = game.ReplicatedStorage.Maps.Map01:Clone()
ClonedMap.Parent = workspace
ClonedMap.Name = "Map"
wait(2)
game.ReplicatedStorage.NumbersPlayersInGame.Value = #game:GetService("Players"):GetPlayers()
table.insert(playeringame, player.UserId)
player.Character.HumanoidRootPart.CFrame = workspace:WaitForChild("Map").Spawns.Spawn.CFrame
player.Character.Humanoid.Died:Connect(function()
game.ReplicatedStorage.NumbersPlayersInGame.Value -= 1
table.remove(playeringame, player.UserId)
end)
repeat
wait()
until game.ReplicatedStorage.NumbersPlayersInGame.Value == 1
if game.ReplicatedStorage.NumbersPlayersInGame.Value == 1 then
Status.Value = "Game Ended"
game.ReplicatedStorage.NumbersPlayersInGame.Value = 0
game.ReplicatedStorage.LastPlayer.Value = playeringame[1]
wait(3)
--print(game.Players:GetPlayerByUserId(playeringame[1]).Name)
print(#playeringame)
end
end)
Also here' the output I put it in a image and here's the image https://i.postimg.cc/ZRRFWvDB/a7.png

1 comment
You probably shouldn't nest what seems to be your game loop code in the PlayerAdded event, because then multiple game loops get created and I don't think you intended to do this. Without the event, you can iterate over all of the players using a for loop and add them each, but for any player that joins after the game started, you can setup a temporary PlayerAdded connection