Even though I converted the string to table, the for statement gets the string and the error occurs

Even though I converted the string to table, the for statement gets the string and the error occurs…

local invFolder = Instance.new("Folder", game.ServerStorage.Inventories)
		invFolder.Name = player.Name..'Inventory' 

		local InvStat = player:WaitForChild("Data_Folder").InvData

		if InvStat.Value ~= '' then
			local LoadedData = HttpService:JSONDecode(InvStat.Value)
			print(LoadedData)
			for i, x in pairs(LoadedData[1]) do
				if x then
					local newString = Instance.new("StringValue", invFolder)
					newString.Name = tostring(x)
				end
			end
		end

Why is this? For your information, we print Loade Data
20240527_003747
Any feedback please!

2 Likes

I believe the issue here is because you’re using pairs instead of ipairs, use ipairs when iterating through arrays (ex: tables with num index:
{true, false, false} -> {[1] = true, [2] = false, [3] = false}

Use ipairs for arrays,

pairs for tables with named indexes:
ex:
{value = true, anothervalue = false, lastvalue = false} -> { ["value"] = true, ["anothervalue"] = false, ["lastvalue"] = false

ipairs will return the i as the number of the index, pairs will return the i as the named value of the index.

This should fix it.

Another thing, in your pairs/ipairs statement, you must put an array or table. You are putting a value/entry of a table. Replace LoadedData[1] with LoadedData.

So try this:

local invFolder = Instance.new("Folder", game.ServerStorage.Inventories)
		invFolder.Name = player.Name..'Inventory' 

		local InvStat = player:WaitForChild("Data_Folder").InvData

		if InvStat.Value ~= '' then
			local LoadedData = HttpService:JSONDecode(InvStat.Value)
			print(LoadedData)
			for i, x in ipairs(LoadedData) do
				if x then
					local newString = Instance.new("StringValue", invFolder)
					newString.Name = tostring(x)
				end
			end
		end
1 Like

What exactly is the error? Which line is it coming from?


The only time the use of an iterator would affect the program is using ipairs on a dictionary. This is because ipairs only iterates through number keys. The use of iterator does not cause the error, however it may not be the best iterator as pairs is significantly slower than say next for example.

1 Like

I’m unsure what error exactly he is referring to, but when you look at the data he is trying to iterate, he is obviously using the wrong iteration function.

And no, that’s not the difference of pairs and Ipairs, as of Roblox Documentation:

  1. pairs() is used with dictionaries. …
  2. pairs() can be used to work with a dictionary element’s key, value, or both. …
  3. ipairs() is used with arrays.

But yes, you can do either or in this situation it doesn’t 100% matter, but when you have a function which fits the description of your data better and works you might as well consider/meet that factor to rule out this being an issue.

The error this dude is facing, is he is trying to iterate an entry in his array, instead of the array, he needs to iterate the array, or he will not have the intended outcome.

His code:
pairs(LoadedData[1]))

What he should do:
either: ipairs(LoadedData) or pairs(LoadedData)

1 Like

I don’t think it’s a problem with pairs, ipairs. It’s crazy. Thank you for your help though.

1 Like

Yes the error isn’t created by the ipairs/pairs function, but the error is you’re passing through an entry (LoadedData[1]) instead of (LoadedData) the actual table.

1 Like

oh I get what you meant now, I thought you said the iterator function itself was the issue. My bad, thanks for clarifying.


Reference of iterators for anyone who wants it (speedwise):

  • After testing, I found that:
  • ipairs, next and no iterator at all all iterated ~0.02 seconds
  • pairs iterates slower at ~0.05 seconds
2 Likes

Yea no worries, I should’ve included a bit more context.

3 Likes

I don’t know how I solved it, but I think it worked anyway! Maybe I forgot LoadedData [1] while putting it in my front door. Anyway, thank you both for your help!

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