For loop out of order

Hello, today I made a code for a project that had this inside:

Code

local texts = {
	
	Text1 = "sample text",
	Text2 = "sample text2",
	Text3 = "sample text3"
}

for k, v in pairs(texts) do
	
	print(k)
	
	task.wait(2)
end

It should print “Text1, Tex2, Text3”, yes?

Output

image

Am I missing something? In the code I mentioned the same thing happens, the loop is out of order and I don’t know why.


In this same code I added another key-value pair and changed the code:

New code

local texts = {
	
	Text1 = 1,
	Text2 = 2,
	Text3 = 3,
	Text4 = 4
}

for k, v in pairs(texts) do
	
	print(k .. " = " .. v)
	
	task.wait(2)
end

Output

image

Starting with the last pair?? Any help appreciated. I am feeling really dumb : (

1 Like

Try this:

local texts = {
	
	Text1 = 1,
	Text2 = 2,
	Text3 = 3,
	Text4 = 4
}

for i = 1, #texts do
    print(texts[i])
end
1 Like

I think you didn’t understood, texts is a dictionary, not an array, things like # do not work with dictionaries. I tried your code, nothing prints out because #texts is 0.

My bad. This works:

local texts = {
	
	Text1 = 1,
	Text2 = 2,
	Text3 = 3,
	Text4 = 4
}
local n = 0

for i, v in pairs(texts) do
	n += 1
end

for i = 1, n do
	print(texts["Text"..i])
end
1 Like

So what do you want to accomplish here? This won’t work and will never, we need to know what you are making this for or is this just a question?

Looping through a dictionary does not go in order because of how they work. Instead use arrays with ipairs.

6 Likes

You should use either for i = 1, #texts or for i,text in ipairs(texts) do

using in pairs does them in a random order I believe.

ipairs is like the for i version of pairs

Also it’s a dictionary. They are ordered as only string values currently

--You need to either do --
local texts = {Text1 = 1, Text2 = 2,Text3 = 3,Text4 = 4}
--this orders them correctly--
--You can also do--
local texts = {}
texts[1] = 1
texts[2] = 2
texts[3] = 3
texts[4] = 4


local texts = {Text1 = "sample text", Text2 = "sample text2", Text3 = "sample text3"}

for k, v in ipairs(texts) do
	
	print(k)
	
	task.wait(2)
end

ipairs is better for arrays and players. (It’s faster than pairs!) It also goes through it in order, which is an extra perk!

Extra info can be found here!

I know it doesn’t work that’s why I made this post lol.

Sorry, I need to sort the dictionary?

?? ipairs isn’t for arrays?

Lua Globals

Yep, I changed it to an array just now.

Try what i said, it worked for me

The first loop is useless, you can just do this for it:

local n = 0
local texts = {
	
	Text1 = 1,
	Text2 = 2,
	Text3 = 3,
	Text4 = 4
}

task.spawn(function()
    repeat
        n += 1
    until n == 4
end)

for i = 1, n do
    print(texts['Text: ']..i)
end

(ok I do not remember what I wrote here, I edited the answer instead of creating a new one)

1 Like

Ok so not to come off rude, but please do at least attempt one of our samples. You won’t get anywhere telling us what was said in the post, instead of telling us what isn’t working from our code.

ipairs is meant for arrays, hence me using one in this fashion:

local texts = {Text1 = "sample text", Text2 = "sample text2", Text3 = "sample text3"}

for k, v in ipairs(texts) do
	print(tostring(k))
	task.wait(2)
end

I will be further discontinuing my replies to this thread, I wish you the best of luck.

2 Likes

I attempted, it works, I just want to get the value without using a number variable to get the key, because I remember doing it without one.

Please do not get offended with what I say, I do not speak a good english :brazil:

but if he add more texts, it wont update automatically

You realize that isn’t an array, right?

{Text1 = "sample text", Text2 = "sample text2", Text3 = "sample text3"}
1 Like
local texts = {Text1 = 1, Text2 = 2, Text3 = 3, Text4 = 4}

for i = 1, math.huge do
	local v = texts["Text"..i]
	if v then
		print(v)
	else
		break
	end
end

If you wanted to avoid using an upvalue and iterating over the dictionary in order to retrieve its length.

May I question why you used math.huge instead of #texts?