Player not getting removed from table

You can write your topic however you want, but you need to answer these questions:

  1. What do you want to achieve? Keep it simple and clear!

i want to make game where you are in a deserted island, and you have to find recourses and stuff…

  1. What is the issue? Include screenshots / videos if possible!

when the player dies the player is not getting removed from the PlayerInRound Table

  1. What solutions have you tried so far? Did you look for solutions on the Developer Hub?

yes

After that, you should include more details if you have any. Try to make your topic as descriptive as possible, so that it’s easier for people to help you!

this is the script, i put prints for debugging

local PlayersInRound = {}
local PlayersInLobby = {}
local Information = game.ReplicatedStorage.Values.Information
local MiniInformation = game.ReplicatedStorage.Values.MiniInformation
local RoundStarted = game.ReplicatedStorage.Values.RoundStarted

game.Players.PlayerAdded:Connect(function(plr)
    MiniInformation.Value = plr.Name.." has joined"
    if RoundStarted.Value == true then
        table.insert(PlayersInRound,plr)
    end
end)

local function PlayerDeath(plr: Player, index: number)
    table.insert(PlayersInLobby,plr)
    table.remove(PlayersInRound,index)
    print("Passssssssssssss")
end

while wait() do
    local AmountPlayers = #game.Players:GetPlayers()
    if AmountPlayers <= 1 then
        Information.Value = "Need More Players "..AmountPlayers.."/2!"
    else
        if RoundStarted.Value == false then
            for i = 30,0,-1 do
                wait(1)
                if i <= 10 then
                    Information.Value = "Round is starting soon!"
                else
                    Information.Value = "Round is starting In "..i.." seconds!"
                end
            end
            Information.Value = "Round Started!"
            wait(3)
            Information.Value = ""
            RoundStarted.Value = true
            for i,v in pairs(game.Players:GetPlayers()) do
                table.insert(PlayersInRound,v)
                local Index = table.find(PlayersInRound,v,nil)
                print(Index,v)
                v.Character:FindFirstChildWhichIsA("Humanoid").Died:Connect(function()
                    print(#PlayersInRound)
                    print(#PlayersInLobby)
                    PlayerDeath(v,Index)
                    print(#PlayersInLobby)
                    print(#PlayersInLobby)
                    print("PlayerDied1")
                end)
            end
        end
    end
end

this is the output:

Please do not ask people to write entire scripts or design entire systems for you. If you can’t answer the three questions above, you should probably pick a different category.

1 Like

Instead of using table.insert(t, v) use t[v] = true. So for example, if you wanted to add a player to the list of players in-game. You’d do as such:

local userId: number = player.UserId

playersInGame[userId] = true :: boolean

Now if you want to remove them from the list, do this:

local userId: number = player.UserId

playersInGame[userId] = nil

ok i will try…

ok???

1 Like

now the tables are empty :
new script

local PlayersInRound = {}
local PlayersInLobby = {}
local Information = game.ReplicatedStorage.Values.Information
local MiniInformation = game.ReplicatedStorage.Values.MiniInformation
local RoundStarted = game.ReplicatedStorage.Values.RoundStarted

game.Players.PlayerAdded:Connect(function(plr)
    MiniInformation.Value = plr.Name.." has joined"
    if RoundStarted.Value == true then
        local userId: number = plr.UserId
        PlayersInRound[userId] = true :: boolean
    end
end)

local function PlayerDeath(plr: Player)
    local userId: number = plr.UserId

    PlayersInRound[userId] = nil
    PlayersInLobby[userId] = true :: boolean
    print("Passssssssssssss")
end

while wait() do
    local AmountPlayers = #game.Players:GetPlayers()
    if AmountPlayers <= 0 then
        Information.Value = "Need More Players "..AmountPlayers.."/2!"
    else
        if RoundStarted.Value == false then
            for i = 30,0,-1 do
                wait(1)
                if i <= 10 then
                    Information.Value = "Round is starting soon!"
                else
                    Information.Value = "Round is starting In "..i.." seconds!"
                end
            end
            Information.Value = "Round Started!"
            wait(3)
            Information.Value = ""
            RoundStarted.Value = true
            for i,v in pairs(game.Players:GetPlayers()) do
                local userId: number = v.UserId
                PlayersInRound[userId] = true :: boolean
                v.Character:FindFirstChildWhichIsA("Humanoid").Died:Connect(function()
                    print(#PlayersInRound)
                    print(#PlayersInLobby)
                    PlayerDeath(v)
                    print(#PlayersInRound)
                    print(#PlayersInLobby)
                    print("PlayerDied1")
                end)
            end
        end
    end
end
1 Like

You’re putting the player in the game as soon as they join:

game.Players.PlayerAdded:Connect(function(plr)
    if RoundStarted.Value == true then
        local userId: number = plr.UserId
        PlayersInRound[userId] = true :: boolean
    end
end)

Try using:

PlayersInLobby[userId] = true :: boolean

What’s the point in type checking that? Like legitimately actually curious why you need to type check true

no i want the player to be in the round after the player joins

Not actually quite sure, I suppose it’s just a habit from other languages like C# where you need to declare the type before initiating the variable.

1 Like

try this:

local function PlayerDeath(plr: Player, index: number)
    table.insert(PlayersInLobby,plr)
    table.remove(PlayersInRound, table.find(PlayersInRound, plr))
    print("Passssssssssssss")
end
2 Likes

I’m unfortunately unable to help you with your round system. I answered the title’s question, which was ‘Player not getting removed from table’ which seemed like the main concern to me.

thanks for your help @CryoPlaysAlot

2 Likes

No problem, glad to see your issue is fixed.

1 Like

That makes sense but that isn’t a variable declaration so that’s mainly why I’m confused XD

1 Like

Honestly hate that you have to do that weird table.find stuff. I wish it would check for both the number index and try to find it in the table automatically before erroring.

1 Like

The thing is that when using table.remove(), it will only find the index of an array and removes the item if it is on that index, which is why i added table.find() instead.

1 Like

( Sorry for bumping ) Yeah I just have those odd ‘phases’ where the style I program in drastically changes and I normally stick to them for a while before I realize I hate the style and I want to replace it again.

I know that. That’s why I’m saying that table.remove() checked for a number index, then automatically do something similar to table.find() instead of having to do it yourself since it looks messy.

2 Likes

That’s fair. Not trying to criticize your style, I’m just wondering since I’m trying to get into type-checking (for a host of reasons) and I was unsure of why you would type-check a static boolean.

1 Like

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.