Scripting Helpers is winding down operations and is now read-only. More info→
Ad
Log in to vote
0

for loop getting different values?

Asked by
trecept 367 Moderation Voter
6 years ago
for i,v in pairs(table) do
    if string.find(v, text) then
        TextLabel1.Text = v
        TextLabel2.Text = v
    end
end

For the script above I want to make it so it loops through a table's values, sees if "text" is found in v and then change two text labels. I want the 1st text label's text to be another value from the other text label. For example if from the string.find 3 different values are returned, I want textlabel1's text to be the 1st value, textlabel2 text to be the 2nd etc. But I've tried everything, inserting the values into a table, but nothing worked. Thank you

1 answer

Log in to vote
0
Answered by 6 years ago

You cannot use "table" for it is a data type.

Put an if statement in your for loop to see if TextLabel1 has anything in it, if not then put v there. If there is something then put it into TextLabel2 instead.

local myTable = {"Shoes","Apples","Bicycle","Shoes"}
local text = "Shoes"

local TextLabel1 = nil
local TextLabel2 = nil

for i,v in pairs(myTable) do
    if string.find(v,text) then
        -- If TextLabel1 already has v stored then it will store 
        -- v into TextLabel2
        if not TextLabel1 then
            TextLabel1 = v
        else
            TextLabel2 = v
        end
    end
end

print(TextLabel1)
print(TextLabel2)
1
The table "table" is not a data type, just a normal table. You are able to set the variable table to anything else if you wish, or iterate through it as pleased, though it's typically not a good choice of a variable name because it can be confusing. Avigant 2374 — 6y
0
I don't understand why it's not a data type. Here - https://www.tutorialspoint.com/lua/lua_data_types.htm it's listed as a data type. Yes it can be used like a variable just like "string". MooMooThalahlah 421 — 6y
Ad

Answer this question