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

Selecting a certain string from my table?

Asked by 5 years ago

Hey guys,

so I've FINALLY decided to put a bit more effort into learning LUA Tables, considering I've been ignoring it for the last few months, and I'm beginning to wonder if it's possible to select a certain string from your table, without knowing the index location.

In this example I've created a table that adds your username once you join, and should remove your name when you leave.

admins = {}

game.Players.PlayerAdded:Connect(function(plr)
    table.insert(admins, plr.Name)
    print("Inserted user "..plr.Name)
end)

game.Players.PlayerRemoving:Connect(function(plr)
    table.remove(admins, plr.Name)
    print("Removed user "..plr.Name)
end)

The joining part works great, yet, obviously, when the user leaves, the table can't select plr.Name because it's not an index value (1,2,3, etc).

Is there any way to work around this issue? PS: This is a SCRIPT, not a LocalScript, trying to expand this to a custom leaderboard in the near future

Thanks!

0
No, you can't remove anithing from a table without using the index Leamir 3138 — 5y

1 answer

Log in to vote
0
Answered by
Vulkarin 581 Moderation Voter
5 years ago
Edited 5 years ago

Hmm well you could do a few things

a dictionary for example:

admins = {}

game.Players.PlayerAdded:Connect(function(pl)
    admins[pl.Name] = true
    print("Inserted user "..pl.Name)
end)

game.Players.PlayerRemoving:Connect(function(pl)
    admins[pl.Name] = nil
    print("Removed user" ..pl.Name)
end)

Or you could actually use a table, you would just have to iterate to get rid of the value:

admins = {}

game.Players.PlayerAdded:Connect(function(plr)
    table.insert(admins, plr.Name)
    print("Inserted user "..plr.Name)
end)

game.Players.PlayerRemoving:Connect(function(plr)
    for index, admin in pairs(admins) do
        if plr.Name == admin then
            table.remove(admins, index)
            break
        end
    end
    print("Removed user "..plr.Name)
end)
0
The dictionary does not seem to work, unless somewhere I'm messing up. Tried to print the table on PlayerAdded, which then comes up with nil. Does it not add the user to the table? User#20989 0 — 5y
0
for the dictionary you would do something like print(admins["YourNameHere"]) and it would say -> true Vulkarin 581 — 5y
0
If you haven't already then I would recommend you thoroughly read over this http://robloxdev.com/articles/Table#iteration Vulkarin 581 — 5y
Ad

Answer this question