Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
COMMUNITY QUESTION

Selecting a certain string from my table?

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!

1 comment

0

No, you can't remove anithing from a table without using the index

1 answer

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)

3 comments

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?

0

for the dictionary you would do something like print(admins["YourNameHere"]) and it would say -> true

0

If you haven't already then I would recommend you thoroughly read over this http://robloxdev.com/articles/Table#iteration

This discussion is preserved as it was. New answers, comments, and votes are closed.