Pairs loop skipping over a part of a dictionary

cant wrap my head around why its doing this, maybe its cuz im extremely tired rn but yeah

table
cooltable = {

	Example1 = {

		thing = "example1";
	},

	Example2 = {

		thing = "example1";

	},

	Example3 = { --skips this table

		thing = "example3";

	},

	Example4 = {

		thing = "example4";

	},

},
loop
-- for this post lets just say thestring is "example3"
local thestring = "example3"
for i,v in pairs(Module.cooltable) do
	print(v.thing)
	print(thestring)
	if v.thing == thestring then
		print(v)
		return
	else
		print("") --just to seperate output for better reading of the prints
	end
end
output
example1 --v.thing
example3 --thestring

example2 --v.thing
example3 --thestring

example4 --v.thing
example3 --thestring

What is it supposed to do instead? Looks like it’s working properly

1 Like

its skipping Example3

output is

example1 --v.thing
example3 --thestring

example2 --v.thing
example3 --thestring

example4 --v.thing
example3 --thestring

instead of

example1 --v.thing
example3 --thestring

example2 --v.thing
example3 --thestring

example3 --v.thing
example3 --thestring
Example3 -- cuz of the conditioned print

That’s because you have this if v.thing == thestring then and set it to return

1 Like

oops, forgot about that return in the other reply

it would still print v tho and the Example4 stuff wouldnt print aswell

Oh, that’s because dictionaries loop in an unordered way. If you want it to be ordered loop you need to have it like this:


cooltable = {

	[1] = {

		thing = "example1";
	},

	[2] = {

		thing = "example1";

	},

	[3] = {

		thing = "example3";

	},

	[4] = {

		thing = "example4";

	},

},
1 Like

for some reason i kept another few lines of code under the v.thing if statement that was matching strings and returning :man_facepalming:

lack of sleep is getting to me

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.