Script literally skips over numeric array


here’s what it prints out in console:

no idea why it just… skips over it. any clues??

We will only be able to help you if you post all the code in the topic…

So if you print(#chararoster) after line print("ee") you will see that the array you are referencing is empty. So the loop reaches its’ exit condition before a single cycle of execution.

In the future you will post your code snippet uncensored so that the people you are asking for help have all the information to help you.

1 Like

This would mean that chararoster is considered an empty table.

here’s the full table, though. note that i minimized some because they’re rather similar. i swapped from a different loop earlier, which was working, but suddenly the numeric version just. doesn’t work.

since @DragRacer31 asked, too, i simply didn’t see the point in adding the rest of the code because even if it was ONLY the print, it should be fine. plus, i don’t want to be ragged on about how messy and unclean my code is since i plan to polish it later.
i suppose though, if you REALLY want to see it, here it is. don’t say i didn’t warn you.

You should be closing all those tables with a }

they are. i minimized it and it doesn’t show that it ends with } for some reason.

Oh, sorry my mistake.

Your problem is that #table only works when there are number indexes. So to get the number of items in this table you would have to do something like so:

local function getAmountOfElements(dictionary)
    local n = 0
    for _ in pairs(dictionary) do 
        n = n + 1 
    end
    return n
end

i can see that working, though, i want the array to pull things in the order it’s written in. i.e. mario first tac last
do you think the dictionary kind of thing would work for that?

You can use pairs to loop through dictionaries. However, it will not be in the order you typed it out in.

for charName, charProps in pairs(chararoster) do
    -- insert code here
end

When it comes to dictionaries order is not preserved whatsoever - so in short - you will not be able to iterate through the indices in the order you have it typed out.

1 Like

weird, cause when i did that, it wasn’t in order for some reason.
i can certainly try again tomorrow morning!! i’ll let you know since i gotta Sleep

dangit! i’ll keep that in mind if i use dictionaries in the future then

If you want an order to which characters are used first, you could use an array of indexes and loop through that instead:

local charaOrder = {
    [1] = "Mario",
    [2] = "Luigi",
    -- all other characters...
}
-- or `for index, charaName in ipairs(charaOrder) do`
for index = 1, #charaOrder do
    local charaName = charaOrder[index]
    local chara = chararoster[charaName]

    -- do stuff with chara
end
2 Likes

i KNEW there was a way i could do something like that!! haha its been a while i since i’ve used lua so im super rusty asking dumb questions

this i think is likely my solution. thanks for everyone else though that offered assistance and whatnot!!

1 Like