Search code examples
luarobloxluau

Vector2 in dictionary doesn't work correctly?


I have made like a game where you have to escape an ancient pyramid and I had one stage where there are different pillars on the floor with Egyptian hieroglyphics on them and in front of of the player there is a sign that says an English word an at the wall there is a paper scroll that the player has to use to translate the English letters to hieroglyphics and find the correct way on the pillars

So I have a dictionary with the English letters and the corresponding Vector2s for the ImageRectOffset because I'm using like a sprite sheet idk. Here is my dictionary

local HieroglyphsTable = {
    ['A'] = Vector2.new(0,0);
    ['B'] = Vector2.new(0,170);
    ['C'] = Vector2.new(0, 340);
    ['D'] = Vector2.new(0, 510);
    ['E'] = Vector2.new(0, 680);
    --and so on
}

Here is the part where I apply the ImageRectOffset Vector2's to the pillar parts:

HieroglyphImageLabel.ImageRectOffset = HieroglyphsTable[math.random(1, DonutLibrary.GetTableLenght(HieroglyphsTable))]

By the way this GetTableLenght is a function that returns the table's length and I tested it that works

My error now is this one: Unable to assign property ImageRectOffset. Vector2 expected, got nil

I've already tried asking ChatGPT but it of course doesn't help that good like humans do, it said something like that I should use Vector3 but why? I've tried all solution I found online but nothing helped me. Can you help me??


Solution

  • The error is telling you that when you tried to set the image offset property, you gave it nothing. This indicates that the way you are fetching the values from the hieroglyph table is incorrect.

    You have stored values into keys like A, B, and C, but the math.random(start, end) function will give you a number like 1, 2, 3. So when you ask for HieroglyphsTable[1], there's nothing there, so it returns nil.

    The simple way to fix this is to store the values as an array, not a dictionary. I'm not sure if you choose a dictionary for the values for some other reason, but this way lets you access values simply with a numeric index.

    local HieroglyphsTable = {
        Vector2.new(0, 0),  --A
        Vector2.new(0, 170),--B
        Vector2.new(0, 340),--C
        Vector2.new(0, 510),--D
        Vector2.new(0, 680),--E
        --and so on
    }
    
    local i = math.random(1, #HieroglyphsTable)
    HieroglyphImageLabel.ImageRectOffset = HieroglyphsTable[i]
    

    However, if it's vital that the original dictionary structure be preserved, then you need a way to randomly select an index in the table. You can do this by putting all of the keys into an array and selecting one randomly, but this way is significantly slower.

    local HieroglyphsTable = {
        ['A'] = Vector2.new(0,0);
        ['B'] = Vector2.new(0,170);
        ['C'] = Vector2.new(0, 340);
        ['D'] = Vector2.new(0, 510);
        ['E'] = Vector2.new(0, 680);
        --and so on
    }
    
    -- hold onto the keys for random access
    local HeiroglyphKeys = {}
    for k, _ in pairs(HeiroglyphsTable) do
        table.insert(HeiroglyphKeys, k)
    end
    
    -- pick a key
    local i = math.random(1, #HeiroglyphKeys)
    local key = HeiroglyphKeys[i]
    
    -- set the image offset
    HieroglyphImageLabel.ImageRectOffset =  HieroglyphsTable[key]