Strings in tables, using table.find()

Hiwi!! Thank you for reading, can someone tell me a good way to access this?
(All code its just an Example typed directly in the post)
I have a table like this:

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

I can use:

for _, i in pairs(list) do
	if i.name == "Crimson" then
		-- do something
	end
end

how to use table.find(list, "Crimson") ?

Related question. Why this Players Table works fine with table.find() and the Color List wont work with table.find() if Im doing the exact same process?

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

That list is created by events, storing the Player instance, and can be accessed without a loop. Just table.find(playersList, playa) by providing another player instance inside the playa var from an event in order to find the index inside the table. But its not working when Im trying to find a string inside the Color Table.

Thank you for your time :3

2 Likes

The reason why your player table worked because of the value that defines it but not the other table for the list I’m horrible with tables :frowning:

1 Like

I realized I sounded confusing so what I mean is the table has defines as you write so you have 3 in the list and in 1 is somthing so you have to see if name = “crimson

1 Like

Still a little confusing sorry :confused:
I really dont get what you mean. The table defines itself as its been written, so I have 3 nodes in the list? and in 1 is something? Values. But you support that I should do the loop and forget about the table.find()? xD Thats what u mean?

I’m confusing so other members will help you shortly but if a method works for you use it

1 Like

Dont worry, Im confusing too, and thank you for the help. But. I kinda need the table.find in order to find the index for that node and delete or make stuff with it… Mmmh Im gonna think why its not working with strings and works with players

Looks like the hierarchy of the table does not allow table.find() to work with it. Even when it’s in the next scope, where it is a dictionary instead. Use a regular for loop to check all the keys. You cannot use table.find() because it’s limited to arrays.

3 Likes

Ahh thx for explaining it more briefly I was really confusing and did not know how to explain

1 Like

But, the Players List is exactly the same hierarchy as the Color List, the only difference, is that the colors list is made by Strings, and the Players List contains Player Instances. Why table.find() works with the Player List and not the Color one?
If I cannot use table.find() to find a string inside “name”, whats the best way to get the node index in order to delete it?
During the loop, make an increment var? and use that var to select a node in the list? seems like a mess… Any way to get the index of that node in color list without failing?

Actually, now when you mention it, you’re not explaining exactly what the table was supposed to do. Can you elaborate the reason why the list look like this?

1 Like

By that question you mean, that maybe I should change the whole approach, cause Im doing a mess with tables? xDD well thats a great answer, cause probably your answer its perfectly right!! I made a mess xD

Kinda fusing systems… Old systems I made for a game and after weeks of building, I got some new tables and I want to “sync” them…
The purpose of the tables is irrelevant for my question. Cause I cant find why table.find is not working with strings and works with player instances.

Im still learning, and Im sure theres a better approach, but, it will take me lots of time to change that. Now I just need to find the index node in a list that looks like the Color List I showed

And sorry for the mess xD

It’s 100% possible I missed what you’re trying to do (I think it’s safe to say at this point everyone is confused.)

From what I can tell, you’re trying to identify the index of the value you’re iterating if it matches a condition, in this case the condition being teamed Crimson.

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

for Iteration, Value in pairs(list) do
	if Value.name == "Violet" then
		print(Iteration) -- output: 1
	end
end

If I missed your point let me know and I’ll just delete this lol

2 Likes

Can you provide the code that is responsible for adding new dictionaries to the player list?

2 Likes

Yup, I did that, maybe the example I gave is a little brief. But yes, doing that loop, and use the index. But Im confused about the order of the list, maybe a player joined while that the loop is happening, made a team, or left the game and changing the list, so using the Iterarion value, to handle the index node, I think it will get mixed with other values, and maybe deleting a different one that is not the one I was searching… Idk… I know nothing about “issues” happening behind curtains.

So I wanted to make sure its the right index, by searching directly into the table and get the index by local index = table.find()

Yup Im using the loop, but, Im a little scared that the values are not correct while scanning the list. So double checking with table.find()

Its just a module that is receiving a Player instance once a player accepted a team with another guy

Woka.TabT = function(a,b,c)
	table.insert(Woka.plays, {play1 = a, play2 = b, team = c})
end

Is there a reason why you’re storing play1 twice?

lmao im just so confused rn

1 Like

Each team is made of 2 players only, So Im just tagging “play1” and “play2”. Because the table exist in the module. I access that table and do things with each player, “play1” has some powers, play2 has different ones, and working on team, sometimes I need to act on one of them specifically, with the table containing those tags, I found it easy for me to handle with that 2 players team

Alternatively, you could set it as a dictionary:

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

The team is now a key and no longer a value in the table. Also you can eliminate the inner keys.

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

Makes totally sense, I never used dictonaries before, I just place everything as a value inside a table. Mainly because while researching, I found bad comments about dicctionaries and that common tables are more flexible. Maybe thats wrong, and I should change to that. Let me think xD cause I have to change a lot in many scripts in order to make it work like that u ,u

1 Like

but… if I got a player instance, how do I look into the table?

Lets say, I dont know the color of the team, but I have his Player Instance and I want to know the color? How to do it?