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:
01 | local function getMostDramatic(axis,up,tab) |
02 | local most = tab [ 0 ] |
03 | for i, v in pairs (tab) do |
04 | if up then |
05 | if axis = = "x" then if v.x>most.x then most = v end |
06 | elseif axis = = "y" then if v.y>most.y then most = v end |
07 | end |
08 | elseif not up then |
09 | if axis = = "x" then if v.x<most.x then most = v end |
10 | elseif axis = = "y" then if v.y<most.y then most = v end |
11 | end |
12 | end |
13 | end |
14 | end |
15 | --The line that is causing the error is right here and not up in my function. |
16 | print (getMostDramatic( "x" , true , { Vector 2. new( 2 , 0 ),Vector 2. new( 10 , 0 ) } ).x) |
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
01 | local function getMostDramatic(axis,up,tab) |
02 | local most = tab [ 0 ] |
03 | for i, v in pairs (tab) do |
04 | if up then |
05 | if axis = = "x" then |
06 | if v.x>most.x then |
07 | most = v |
08 | end |
09 | elseif axis = = "y" then |
10 | if v.y>most.y then |
11 | most = v |
12 | end |
13 | end |
14 | elseif not up then |
15 | if axis = = "x" then |
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.