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
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.
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
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.
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:
pairs() is used with dictionaries. …
pairs() can be used to work with a dictionary element’s key, value, or both. …
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)
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.
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!