Ipairs don't work

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

it is recommended to use ipairs while iterating through values in an array , when you need speed , it iterates in index value pairs rather than key value pairs which reduces the time taken to return the values, so to return a value from an array for example, it would take lesser time using ipairs then with iterating though key value pairs, using pairs instead.

im veryyyy bad at tables and i need to learn alot of stufs. The newest thing i learn about it was table.getn()

getn is deprecated and should not be used. The length operator # exists

1 Like