Strings in tables, using table.find()

That’s just microoptimization that does not affect the game in any way, nor have tutorials like these made any statement that oppose dictionaries:


For searching the player instance, you did make mention of the table.find(). Try that at least once. If that doesn’t work, run a linear scan through the entire table searching through all the values per keys.

1 Like

Then if I have a dicctionary like

Woka.TabT = function(a,b,c)
	Woka.plays[c] = {a, b}
end

By only using this, I will get the “name” of the team color or a number? If works… cause you stated that if doesnt work, I should use the loop, and that returns me to the beginning, if Im gonna use the loop then, theres no reason to change anything, cause I can use the loop right now and its working. I wanted to use table.find() instead of the loop

local index = table.find(list, PlayerInstance)
print(index)
"OUTPUT: " SomeColor or xNumber?

I still have the doubt why table.find() its perfectly working on find a player Instance on the table, and its unable to find a String. The tables are equal, value = “something”

A combination of it instead would suit it:

local function getTeamFromPlayer(player)
    for team, players in pairs(list) do
        if table.find(players, player) then
            return team
        end
    end
end

Remember to:

External Media
1 Like

xDDD funny!!

Yup, Im always trying it and see it, (I like experiments more than tutorials or asking), sometimes I take a little rest and ask some stuff on forum. Maybe instead of changing to dicctionaries, I just do the loop that I didnt wanted to do in order to make it work, and forget about the table.find()… unless I wanna change a lot in all other scripts :3

Thank you all guys :3

Maybe this is useful, but I’m not an expert so I could be wrong. Feel free to correct me.

“table.find” internally is implemented with a loop and an if, something like this:

function table.find (Table, Value)
	for k, v in ipairs(Table) do
		if v == Value then
			return k
		end
	end
	return nil
end

so doing “table.find(playersList, playa)” really shouldn’t work. Not working with strings is fine. So “table.find” can’t do what you want and you should keep the loop as it was in the beginning.

I don’t think you should worry about this. Scripts (and coroutines) create threads (independent pieces of code that are ready to be executed in parallel or something like that). Threads can share memory, but only one thread has access to a certain memory location at a certain time. In simple words, if a code is using a variable (that stores a table for example) no other code can change that variable at that time (it has to wait until the first code stops handling that variable).

I don’t really know how roblox works internally, but thinking this way was quite useful to me.

2 Likes

Thank you so much for your reply @MillaGamerF!
I like that way that you are showing, seems cleaner than the way I used to code. (I’m still learning)

The bad about this, is that I intended to not make a loop in order to find the value and index.
Its great to know that there are independant Threads working behind. I asked a question about Threads before in devForum. And yup, everyone told me that I had nothing to worry about. (Until now I havent any problems with that)… But…

I used a couple of game design/scripting engines before, like Unity and another weird one based on GTA SA:MTA… Cause Im still learning sometimes I create a huge mess with lots of systems that works and everything, but I had that problem before, like. While a script is reading something, another script change something inside something and suddenly I got a weird value that should not happen… (Probably I was more inexperienced and surely my mess was worst than the actual mess I do xD) But Im still a little scared about that stuff, so Im taking extra locks in order to that not happen.

I dont know what exactly you mean, cause table.find(playerList, playa) its what Im using and its working great. Finding the player value and the whole node, by only providing a player instance, without doing any loop. Thats why it was weird that is not working while trying to find a String, and working perfect while finding an Instance.
You mean “shouldnt work” as, if its working, its an error, cause that should NOT work?

Yesterday in the end, I decided to use the loop like normal ppl does xD
While using the loop, theres no reason to use table.find inside it, not for now, not having any problems with finding the value and index using the loop. But, adding the table.find inside the loop still a good idea, sounds like being sure the data is not getting mixed as in other engines I tested.

Thank you so much for your help, and sorry for writting this much… I love to write :v

wow. Unity, GTA SA: MTA. Awesome! I hope one day to learn other engines. about this code

I mean, we should think that table.find is implemented this way, that is, using “table.find” is the same as using that code.

Maybe I’m not getting it right, but anyway, here’s my explanation:

local list = {
	{name = "Violet", took = false},
	{name = "Crimson", took = false},
	{name = "Gray", took = false},
}

local playersList = {
    {play1 = Dev_Peashie, play2 = AnotherName, team = "Crimson"},
    {play1 = BlaBla, play2 = BlaBla2, team = "Violet"}
}

You have these two arrays (tables), whose contents are only tables. Now, if you do “table.find(list, “Crimson”)” or “table.find(playersList, playa)”, the result of both should be nil because in the arrays there are neither strings nor players (only tables).

I find it quite strange that “table.find(playersList, playa)” works for you. But as I said, I could be misunderstanding.

1 Like

Yup, your explanation is right. And using table.find() uses a loop indeed.
About why its working for me on one array and wont work for the “colors” array, well, probably Im missing something inside my mess, and maybe Im finding the table inside the array before using table.find() to get the player Instance inside that table.

The weird thing is that I replicated the same process for the “color” table and one function works fine and the other one gets nil. But, theres no problem, by using the loop everythig is working fine. I will experiment later to really find whats happening inside my mess xD
Thank you so much for your help @MillaGamerF