Ipairs don't work

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()?

It’s not warning in the loop because the table is empty.

1 Like

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.

5 Likes

the table isnt empty and it is a array

i use arrays and i tryed pairs() and it work but i want to use ipears() becouse they are faster

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.

2 Likes

If it worked with pairs, then it’s because the table has no array part.

Test this for me real quick

for key, value in pairs(DataAndValues) do
    print(type(key), key, value)
end
1 Like

it have a array part:

DataAndValues[tostring(Name)] = tostring(Key)

this is in another function

No, that makes it a dictionary. A table’s dictionary part is an unordered mapping of key-value pairs. You’re adding a string key.

Also you should try not calling tostring on things you know are already strings.

2 Likes

but they can be a number and then what array means?

An array is an ordered collection of values. A list.

1 Like

how u can create them? 30chars

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.

2 Likes

thanks u! now i know when i should use ipairs() (for me never lol)

1 Like

No, you should use ipairs when you want to traverse the array part of the table.

Also it’s in the roblox lua style guide to do this.
https://roblox.github.io/lua-style-guide/#tables

but he say it makes the scirpt a bit faster but it still not be more faster

What do you want to say? I didn’t get you

he say with ipairs make the script a bit faster but not enouth for humman eye unless u use every frame

There wouldn’t be a reason to use it every frame. If you’re in a case where you need this, something is wrong.

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.

3 Likes