Hey there.
Trying to scroll through table 'F' in my script
1 | local E = A:WaitForChild( 'C' ):WaitForChild( 'H' ) |
2 | local F = { "your" , "my" , "we" , "yeet" , } |
3 |
4 | for x 123 ,x 124 in pairs (F) do |
5 | if E.Text = = x 124 then |
6 | E.Text = F [ x 123 + 1 ] |
7 | end |
8 | end |
I plan to put this in a repeat to run it over and over again, where it fades in and shows the next message, then fades out and shows the next one
I thought where I put ``E.Text = F[x123 + 1] it would make E.Text equal to F[2], since x123 is one at the moment.
Where am I wrong
EDIT:
I updated my code to this, but I get 'nil' - you also get some bonus content
01 | repeat |
02 | for nn = 0 , 1 , 0.05 do |
03 | E.TextTransparency = nn |
04 | wait(. 05 ) |
05 | end |
06 |
07 | local abcd = #F |
08 | for a,b in pairs (F) do |
09 | if E.Text = = b then |
10 | local function yeet 1 () |
11 | if b = = #F then |
12 | return 1 |
13 | else |
14 | return b + 1 |
15 | end |
You're not wrong. It's printing as expected. Also, I'm using ipairs because your order when going through the table might be different than mine. With ipairs it'll iterate in order.
You're already finding the positions of items in the table with x123
01 | local E = "your" |
02 | local F = { "your" , "my" , "we" , "yeet" , } |
03 |
04 | for x 123 ,x 124 in ipairs (F) do |
05 | if E = = x 124 then |
06 | E = F [ x 123 + 1 ] |
07 | print ( "Matched at: " .. x 123 .. " with " .. tostring (E) .. " and " .. tostring (x 124 )) |
08 | else |
09 | print ( "No match: " .. x 123 ) |
10 | end |
11 | end |
My results:
Matched at 1 with my and your
Matched at 2 with we and my
Matched at 3 with yeet and we
Matched at 4 with nil and yeet
At index 1 it matched "your" to "your" so E becomes "my"
At index 2 it matched "my" to "my" so E becomes "we" and so on
To find the key of an arbitrary value in a table, we can use linear search:
01 | local function LinearSearch(TargetTable, TargetValue) |
02 | for Key, Value in pairs (TargetTable) do |
03 | if Value = = TargetValue then |
04 | return Key |
05 | end |
06 | end |
07 | end |
08 |
09 | local TargetTable = { "a" , "b" , "c" } |
10 | print (LinearSearch(TargetTable, "b" )) --> 2 |
With linear search, the number of possible steps we have to take through the table in the worst case (value is at the last element iterated over in the table) increases linearly with the growth of the number of elements in a table.