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

Why is table.remove() not removing players from the table?

Asked by 7 years ago
Edited 7 years ago

So I'm trying to make it so that my script adds players into a table, right. Now, every time a player dies, I want to remove it from that table. Now whenever I do this, it doesnt work.

for i,v in pairs(Player) do 
            enabled = false
            v.Character.Humanoid.Changed:connect(function() 
                if v.Character.Humanoid.Health == 0 and enabled == false then 
                    enabled = true
                    print(v.Name.." has been killed.")
                    table.remove(Player,    v) -- Says number expected, got object can u help with that
                    print(#Player)
                    if #Player == 1 or #Player == 0 then 
                        Game_In_Progress = false
                        print("Game Has Ended")
                    end
                end
            end)
        end 
0
How is Player defined? Can you elaborate more on how it doesn't work? Are there any errors present that we should know about? Any other information that would help us recreate this issue? M39a9am3R 3210 — 7y
0
Sorry, I rambled on there, I didn't notice the comment on line 7 before posting. Still, how is Player defined? M39a9am3R 3210 — 7y

2 answers

Log in to vote
0
Answered by 7 years ago

Try changing it to:

-- code changing
If v.Character. Humanoid. Health == 0 and enabled == false then
    enabled = true
    print(v.Name.. " has been killed.")
    table.remove( -- Table name here, i -- You should use 'i' because you are at the position of the object you want to move) 
    print(#Player)
-- rest of code

So what's happening is you're already inside the table looping through, so you won't find the "Player" really, you will find its position and you need to get rid of that. So it should be 'i' and not 'Player'.

0
Thanks :) I forgot about using i lol RedPandaEmperor 45 — 7y
Ad
Log in to vote
0
Answered by 7 years ago

Humanoid.Died fires whenever a humanoid's health reaches 0, so you can use that as a replacement. Also, table.remove's second argument is the index to "remove". You have to find the player that died's spot on the table (via loop or another method).

for i,v in pairs(Player) do 
            enabled = false
            v.Character.Humanoid.Died:connect(function() 
                if not enabled then 
                    enabled = true
                    print(v.Name.." has been killed.")
            for b,c in pairs(Player) do --assuming Player is a table
            if v == c then
                    table.remove(Player,    b) 
            break
                end
               end
                    print(#Player)
                    if #Player <= 1 then 
                        Game_In_Progress = false
                        print("Game Has Ended")
                    end
                end
            end)
        end 

Answer this question