Attempt to index local 'hex' (a nil value)

I am trying to choose randomobject (color) from table and then applying it to newrect but i am getting the follwoing error

Attempt to index local ‘hex’ (a nil value)

File: main.lua

Line: 17

stack traceback:

main.lua:17: in function ‘convHexColor’

main.lua:40: in function ‘_listener’

?: in function <?:167>

?: in function <?:205>

Here is the code

local physics = require( “physics” )

physics.start()

display.setDefault(“background”, 1, 0, 0)

function randomcolor()

local colors = { “2ecc71”, “3498db”, “9b59b6”, “2c3e50”, “e74c3c” }

    local  choosecolor = color[math.random(1,5)]    

    end

  

    local function addnewobject()

    local bombstartnew = display.newRect( 150, -340, 50, 50)

physics.addBody( bombstartnew )

bombstartnew.enterFrame = offscreen

   

   bombstartnew:setFillColor( convHexColor( choosecolors ))

Runtime:addEventListener( “enterFrame”, bombstartnew )

end

timer.performWithDelay( 500, addnewobject, 0 )

Where the code for convHexColor()?

Rob

Hi rob here is the code function convHexColor(hex) local r = hex:sub(1, 2) local g = hex:sub(3, 4) local b = hex:sub(5, 6) return tonumber(r, 16), tonumber(g, 16), tonumber(b, 16) end

First, if you could, when you post code, please click on the blue <> button and paste your code in there.

If you read the error message, the value “hex” is nil at line 17 in the function convHexColor(). You need to ask yourself why this function thinks “hex” is nil.

These two lines are the culprit:

local &nbsp;choosecolor = color[math.random(1,5)] &nbsp; bombstartnew:setFillColor( convHexColor( choosecolors ))

In the first line choosecolor is singular, in the 2nd line it’s plural.

Rob

Fixed it but still get same error

Would unpack function useful in the case .If yes how can use it thanks

Same error? Same line number?

Yes

Can you post your current code. Please click on the blue <> button and paste the code into that windows please.

Thanks

Rob

local physics = require( "physics" ) physics.start() display.setDefault("background", 1, 0, 0) function randomcolor() colorlist = { "#27ae60", "#e74c3c", "#e74c3c", "#34495e", "#7f8c8d" } &nbsp; choosecolor = colorlist[math.random(1,5)] end function convHexColor(hex) &nbsp; local r = hex:sub(1, 2) &nbsp; local g = hex:sub(3, 4) &nbsp; local b = hex:sub(5, 6) &nbsp; return tonumber(r, 16), tonumber(g, 16), tonumber(b, 16) end &nbsp;local bombstartnew = display.newRect( 150, -340, 50, 50) physics.addBody( bombstartnew ) bombstartnew.enterFrame = offscreen &nbsp; &nbsp; &nbsp; &nbsp;bombstartnew:setFillColor( convHexColor( randomcolor )) Runtime:addEventListener( "enterFrame", bombstartnew ) end timer.performWithDelay( 500, addnewobject, 0 )

The way you have this:

bombstartnew:setFillColor( convHexColor( randomcolor ))

you are passing the address of the function randomcolor() to convHexColor. The address of a function is a number (memory address) not a string of hex codes.

Perhaps you meant to do:

bombstartnew:setFillColor( convHexColor( randomcolor()  ))

where you actually call the function randomcolor() with the hopes that it would return you a value, but that still won’t work because the function randomcolor() sets a variable but doesn’t actually return anything.

A rewrite of randomcolor() to be:

function randomcolor() local colorlist = { "#27ae60", "#e74c3c", "#e74c3c", "#34495e", "#7f8c8d" } local choosecolor = colorlist[math.random(1,5)] return choosecolor end

would solve that and then:

bombstartnew:setFillColor( convHexColor( randomcolor() ) )

would likely do what you need it to do.

Rob

Still get the same error and same line

here is code

local physics = require( "physics" ) physics.start() display.setDefault("background", 1, 0, 0) function randomcolor() local colorlist = { "#27ae60", "#e74c3c", "#e74c3c", "#34495e", "#7f8c8d" } local choosecolor = colorlist[math.random(1,5)] return choosecolor end function convHexColor(hex) local r = hex:sub(1, 2) local g = hex:sub(3, 4) local b = hex:sub(5, 6) return tonumber(r, 16), tonumber(g, 16), tonumber(b, 16) end function addnewobject() local bombstartnew = display.newRect( 150, -340, 50, 50) physics.addBody( bombstartnew ) bombstartnew.enterFrame = offscreen bombstartnew:setFillColor( convHexColor( randomcolor ) ) Runtime:addEventListener( "enterFrame", bombstartnew ) end timer.performWithDelay( 500, addnewobject , 0 )

ok it now works without error but the object is blue and when i put print(randomcolor) i get function: 0x7faac32f2fb0 instead of a hex value

Because you are still passing the address of the function, not the return value.  Please change this line to read:

bombstartnew:setFillColor( convHexColor( randomcolor() ) )  --<— see the added parenthesis?

Where the code for convHexColor()?

Rob

Hi rob here is the code function convHexColor(hex) local r = hex:sub(1, 2) local g = hex:sub(3, 4) local b = hex:sub(5, 6) return tonumber(r, 16), tonumber(g, 16), tonumber(b, 16) end

First, if you could, when you post code, please click on the blue <> button and paste your code in there.

If you read the error message, the value “hex” is nil at line 17 in the function convHexColor(). You need to ask yourself why this function thinks “hex” is nil.

These two lines are the culprit:

local &nbsp;choosecolor = color[math.random(1,5)] &nbsp; bombstartnew:setFillColor( convHexColor( choosecolors ))

In the first line choosecolor is singular, in the 2nd line it’s plural.

Rob

Fixed it but still get same error

Would unpack function useful in the case .If yes how can use it thanks

Same error? Same line number?