table.find()
requires two different things to be input:
- The table that it should look through
- The thing to look for (which in this case, is the name of one of the objects)
It only works with arrays (which are tables that have numerical indexes). An array could be compared to the pages of a book, in a way. For example, if you had a book with 3 pages, the index would be the number of the current page (1, 2, or 3), and its value would be the words on that page.
The two tables in the example from my previous post are both arrays; here’s what the “SecondTierNames” table would look like if you printed it out:
-- Array; works with table.find()
{
[1] = "Closed",
[2] = "EyeOpen",
[3] = "Pupil"
}
However, if it was a dictionary, which consists of key / value pairs that may not be numbers, table.find()
would not work. Here’s an example of what table.find()
would not be able to look through:
-- Dictionary; does not work with table.find()
{
["ObjectName"] = "Closed",
["AnotherObjectName"] = "EyeOpen",
["FinalObjectName"] = "Pupil"
}
When we call table.find(SecondTierNames, v.Name)
later on in the script, that goes through everything on the first layer of the table and checks if any of the values matches v.Name
. If it does, the table.find()
call will return the index / the position in the list that it was found at. So if v.Name
was “EyeOpen”, the table.find()
call would return “2”.
In this case, we only need to know if it found a match in the table, which is why the conditional statement is only checking if table.find(SecondTierNames, v.Name)
returns a truthy value (which could be true
, a string, a number, etc.) If it doesn’t find a match in the table, it will return nil
, which would fail the condition.
It’s somewhat similar to how the original code worked, where it manually checked if v.Name
equalled any of the specific names. If it didn’t, it would return false
and then check the next name. The table.find()
call essentially does the same thing, except it returns nil
if it doesn’t find any match (with nil
meaning “non-existent” / nothingness).
If you had a nested table, like the following, the table.find()
call would work a bit differently:
local exampleTable = {
[1] = {"Closed", "EyeOpen", "Pupil"}
}
If you used table.find(exampleTable, "ValueToLookFor")
, it’d only be able to look through the first layer of the “exampleTable”. Since the value at index #1 is a table, table.find()
would not return a match when looking for a string / the name of an object.
In order to search through the nested table, you would need to refer to it somehow. For this example, it could be modified to table.find(exampleTable[1], "ValueToLookFor")
, as exampleTable[1]
would reference the value that was stored at index #1 (which is the nested table) in order to look through that table in particular.
For a super simple explanation, task.spawn()
is essentially creating a “new script” in the sense that each time it is called, it will run the code within it separately from everything else that is happening.
As an analogy, imagine that the loop represents the actions that a cashier is responsible for at the checkout line in a store. They have to scan every item on the conveyor belt (in this case, let’s say that’s equivalent to checking if the script can find the name of the given object in the table), and then after that, they have to place the item into a bag so it’s easy for the customer to take it with them (which for this example, will be equivalent to creating and playing the Tween for the Image’s transparency).
However, if the line is long and they need to get work done more quickly, they might need some help from a co-worker. So, they call someone over to help them with bagging the items so that it doesn’t take as long to help each customer. The task.spawn()
call in the function could be equivalent to that situation, because the co-worker is able to start bagging items while the cashier is still scanning items. The customer no longer needs to wait for the cashier to finish placing the item into a bag before scanning the next one since their co-worker is able to help at the exact same time.
When looking back at the script, this means that when the code reaches the section of the loop where it knows that it needs to create a Tween for one of the GuiObjects, it runs the relevant code within the task.spawn()
call to tell the game that it can do that at the same time. This way, the loop will be able to continue running, immediately looking through the rest of the items within the object.
Essentially:
- Script loops through the object
- As soon as it finds a match,
task.spawn()
is called
- The code within the
task.spawn()
call begins to run
- The script continues looping through the next objects while the Tween for the previous object plays
This happens so fast that it’ll look like all of the Tweens were created at the exact same moment in time, but it is still creating each Tween separately (on each iteration of the loop where there is a match) and it plays it immediately after creation (so it is not waiting until it has looped through every matching item before beginning to update the ImageTransparency
).
I hope this makes sense!
No worries! I love explaining how stuff works so this is always fun