1

# How to get a string/name from a table?

CodeWon 121
2 months ago

So I'm making a ban script and I can't figure out how to get names from the table of banned players

i tried this it didn't work:

if player.Name == table.find(banned) then


(banned is table/list of banned players)

How could I do this?

0
do you need to name of the player in a string format? Or can you not fetch the player at all? MOREHOURSOFFUN 104 — 2mo

1
TGazza 849
2 months ago

table.find() takes more than one variable, it accepts the table (haystack) you're searching within and a search (needle) string

for example

if player.Name == table.find(banned) then


will work but it will return either nil or simple error out to your output as you've discovered.

to fix this you need to specify the needle or in your case the player.name. To do this simply change what you have to something like this:

if table.find(banned,player.Name) ~= nil then
-- do stuff
end


This will search your banned list for the player.Name and return the index of where the name if it exists in your banned list if it does not exist in the list it will return as a nil

You can also specify where to start the search in your list, for instance if you know something's at the end of your list but don't want to cycle through all the list for some reason you can call the function like:

if table.find(banned,player.Name,25) ~= nil then
-- do stuff
end


The search will start it search from the index 25 onwards to the end of the table/list

if you need more reference you can look up the table.functions on the roblox wiki here: https://developer.roblox.com/en-us/api-reference/lua-docs/table

hope this helps! :)

0
It's better you use a Dictionary for reference ease. table.find() has a time complexity of O(n)—where n is relative to table size—which can become very expensive in the future with an ever-increasing ban repository. Ziffixture 6785 — 2mo