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

How do I fix this directory bug with a for i, v in pairs loop?

AvailableSpots = {
	["1"] = true;
	["2"] = true;
	["3"] = true;
	
}

for i,v  in pairs(AvailableSpots) do
	print(i)
	print(v)
end

output

  1
  true
  3
  true
  2
  true

Why does it skip to 3? How would I fix this?

1 comment

0

This is probably because the pairs function doesn't have a specified key order, if you want a specified key order, convert your table into an array, then use ipairs  https://stackoverflow.com/questions/55108794/what-is-the-difference-of-pairs-vs-ipairs-in-lua

1 answer

The order a table is written in is not preserved unless your keys are numerical. In your case, this shouldn't be a problem, as your string keys can just as easily be numbers:

AvailableSpots = {
    [1] = true;
    [2] = true;
    [3] = true;
}

for i, v in pairs(AvailableSpots) do
    print(i, v)
end

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