Scripting Helpers is winding down operations and is now read-only. More info→
Ad
Log in to vote
1

Unknown Cause for Error?

Asked by 9 years ago

Every time I run the print statement I get an error (at the line of the print not in my function) that says " attempt to index a nil value" If anyone could explain to me what's causing this I would very much appreciate it. c:

local function getMostDramatic(axis,up,tab)
    local most=tab[0]
    for i, v in pairs(tab) do
        if up then
            if axis=="x" then if v.x>most.x then most=v end
            elseif axis=="y" then if v.y>most.y then most=v end
            end
        elseif not up then
            if axis=="x" then if v.x<most.x then most=v end
            elseif axis=="y" then if v.y<most.y then most=v end
            end
        end
    end
end
--The line that is causing the error is right here and not up in my function.
print(getMostDramatic("x",true,{Vector2.new(2,0),Vector2.new(10,0)}).x)
0
I'm confused as to why it wasn't telling me there was an error on line 5,6,9, or 10 because tables actually start counting at 1 and so that was the nil value ... anywas c: I'm not getting this error anymore so idc Prohibetur 70 — 9y

1 answer

Log in to vote
1
Answered by 9 years ago

You either have too many ends, or not enough ends...let me see... Using tabs can help you organize your code. Spaces make it hard to read

local function getMostDramatic(axis,up,tab)
    local most=tab[0]
    for i, v in pairs(tab) do
        if up then
            if axis=="x" then
                if v.x>most.x then 
                    most=v 
                end
            elseif axis=="y" then 
                if v.y>most.y then
                    most=v
                end
            end
        elseif not up then
            if axis=="x" then 
                if v.x<most.x then
                    most=v 
                end
            elseif axis=="y" then 
                if v.y<most.y then
                    most=v 
                end
            end
        end
    end
end
print(getMostDramatic("x",true,{Vector2.new(2,0),Vector2.new(10,0)}).x)

Did you see what I fixed? You missed not one, but TWO ends!

Organize your code so that you don't make simple mistakes. Trust me, I catch myself with not enough ends a lot.

0
Where are you claiming that I missed 2 ends? ctrl+f showed that we both had 9 ends in our code ... Prohibetur 70 — 9y
0
Odd...I removed two ends. Did you change your code? Run it again and tell me the error once more lightpower26 399 — 9y
Ad

Answer this question