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

Removing a player from a global table?

Asked by
Seenit 80
10 years ago
game.Players.PlayerRemoving:connect(function(Player)
    table.remove(_G.Plyrs, Player.Name)
end)

This is what I am doing but it's expecting a number rather than a string. Anyway to remove a player from a table easily?

1 answer

Log in to vote
1
Answered by
Goulstem 8144 Badge of Merit Moderation Voter Administrator Community Moderator
10 years ago

That's because the table.remove method's second argument is the index value, meaning it won't search through the table to remove what you want..

Say I had this table;

tb = {1,2,3,4,5}

And I did;

tb = {"Hi","Bob",2}

print(table.concat(tb," "))
> Hi Bob 2

table.remove(tb,2)
print(table.concat(tb," "))
> Hi 2

It won't remove the index in the table named 2 it will remove the index in the table at that value.

So to fix your problem then you could do this;

val = nil
for i,v in pairs(_G.Plyrs)
    if v == Player.Name then
        val = i
    end
end
--Now the variable 'val' will have the index number that you need to remove the desired object from your table.

table.remove(_G.Plyrs,val)

-Goulstem

0
This worked in playsolo but not online. It says on the "for i,v in pairs(_G.Plyrs)" that it was expecting a table but got nil. Seenit 80 — 10y
0
Uhhm, I'm not exactly sure what the problem would be. That means the the table doesn't exist anymore.. Maybe this is loading at the same time a the creation of the table, add a wait(5) at the top of the script. Goulstem 8144 — 10y
Ad

Answer this question