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?
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
Can you provide the code that is responsible for adding new dictionaries to the player list?
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
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
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
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?
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.
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 MediaxDDD 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.
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.
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