hi, i wanted to make a CustomDataStore but it don’t save the data idk why.
my code:
function DataModule:SaveAllValues(parent,player)
warn('a') -- it warns a
for Name,Key in ipairs(DataAndValues) do
warn('saving '..player..' data named '..tostring(Name)..'with key '..tostring(Key)) -- it don't warn nothing
DataModule:SaveValue(player,Key,Name,parent[Name].Value)
end
end
i think becouse i use instead of
DataAndValues = {}
this:
DataAndValues = table.create(25)
is the table.create() problem? ive tried to get what DataAndValues stores and it get good, i use bad ipairs()?
ipairs only works for arrays. Judging by the fact it’s not running, you’re probably trying to use it on a dictionary or mixed table. See if using pairs instead works.
If you use pairs, does it print anything? ipairs only works if it’s an array starting at 1 with no gaps, so the issue might be that the index at 1 is nil.
You’re running tostring on it, which makes it a string. Just get rid of the tostring call and replace it with DataAndValues[Name] = Key, assuming that Name is a number. If Name isn’t a number, then this is just a dictionary like @incapaxx and I have been saying, so you can’t use ipairs. The speed difference is minor enough that you don’t have to worry about it anyway unless you’re calling it every frame or something.
An array is just where the indexes are numbers, like this: x[1] = 2
Edit: Here’s the PiL entry for arrays, which gives a better description including code samples: Programming in Lua : 11.1. Technically it’s not all tables with numeric indexes, but for most purposes that definition will be fine.
I think you misunderstood what I was trying to say. It is faster when you’re trying to loop through the numeric part of a table, but the difference isn’t very significant. You should definitely use ipairs whenever possible, but you shouldn’t restructure your dictionary to be an array just so that you can take advantage of the speed difference. It definitely isn’t a major difference though, yeah.