Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
COMMUNITY QUESTION

for loop getting different values?

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

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)

2 comments

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.

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".

This discussion is preserved as it was. New answers, comments, and votes are closed.