Scripting Helpers is winding down operations and is now read-only. More info→
Ad
Log in to vote
0

When removing the player from the table using table.remove on line 32?

Asked by 2 years ago
Edited 2 years ago

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

0
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 Rare_tendo 3000 — 2y

1 answer

Log in to vote
0
Answered by
bdam3000 125
2 years ago
Edited 2 years ago

table.remove removes the index value of the table you set in the second parameter, and you can't actually remove it by using the player's user ID.

you can find the index of the value you are looking for by using table.find

try replacing line 32 with this:

table.remove(playeringame, table.find(playeringame, player.UserId))
0
Just a suggestion, but you should probably validate that the player does exist in the array because table.remove will automatically remove the last element of the table when the 2nd arg of it is nil. Doesn't matter in this case particular;y, but it does matter if OP considers other ways the player may be eliminated Rare_tendo 3000 — 2y
Ad

Answer this question