What is wrong with my code?

[lua] Rand = math.random;

local function randomnumber()
randomn = Rand(0, 10)
print (randomn)
return randomn
end
–randomnumber()
Timerrandom = timer.performWithDelay(1000,randomnumber, 0)
local backgroundtable = {}
local bg1 = {}
bg1 = “1.png”;
table.insert(backgroundtable, bg1);

local bg2 = {}
bg2 = “2.png”;
table.insert(backgroundtable, bg2);

local bg3 = {}
bg3 = “3.png”;
table.insert(backgroundtable, bg3);

local bg4 = {}
bg4 = “4.png”;
table.insert(backgroundtable, bg4);

local bg5 = {}
bg5 = “5.png”;
table.insert(backgroundtable, bg5);

local bg6 = {}
bg6 = “6.png”;
table.insert(backgroundtable, bg6);

local bg7 = {}
bg7 = “7.png”;
table.insert(backgroundtable, bg7);

local bg8 = {}
bg8 = “8.png”;
table.insert(backgroundtable, bg8);

local bg9 = {}
bg9 = “9.png”;
table.insert(backgroundtable, bg9);

local bg10 = {}
bg10 = “10.png”;
table.insert(backgroundtable, bg10);

s1 = {}
local s1 = backgroundtable[randomn]
local s1 = display.newImage(backgroundtable[randomn])
s1.myName = “bg” s1.rotation = 90
s1:setReferencePoint(display.CenterLeftReferencePoint)
s1.x = 160
s1.y = 0

s2 = {}
local s2 = backgroundtable[randomn]
local s2 = display.newImage(backgroundtable[randomn])
– Set image path for object

s2.myName = “bg2”
s2.rotation = 90
s2:setReferencePoint(display.CenterLeftReferencePoint)
s2.x = 160
s2.y = 480

local tPrevious = system.getTimer()
local function move(event)
local tDelta = event.time - tPrevious
tPrevious = event.time

local yOffset = ( 0.15 * tDelta )

s1.y = s1.y - yOffset
s2.y = s2.y - yOffset

if (s1.y + s1.contentHeight-1) < 0 then
s1:translate( 0, 480 * 2)
end
if (s2.y + s2.contentHeight-1) < 0 then
s2:translate(0, 480 * 2)
end
end
Runtime:addEventListener( “enterFrame”, move )[/lua]

I keep getting , attempt to index local ‘s1’ (a nil value)
It works perfect without the timer, how can I fix this?

Any help is much appreciated

if it helps, here is a zip file http://www.mediafire.com/?tnhydtr5pyrnxtn

[import]uid: 23546 topic_id: 12771 reply_id: 312771[/import]

At what row do you get the error? [import]uid: 24111 topic_id: 12771 reply_id: 46829[/import]

Error starts at line 58. [import]uid: 23546 topic_id: 12771 reply_id: 46859[/import]

It seems to be a problem with the randomn variable, which actually isn’t changing and with the function you only print it and setting it up again and again… what you could do is setting the randomn variable outside, something like this:

[lua]-- your other code
randomn = Rand(0,10)
– your other code[/lua]

Hope it helps! [import]uid: 58067 topic_id: 12771 reply_id: 46896[/import]

Thanks for the reply. But if I keep the variable outside, as I mentioned earlier the code works, but its the same background again and again. The purpose of the function was to plug another random number from 1 to 10 into the variable randomn everytime it was called, to call the function every couple seconds, I set up a timer. At least that was how the code was supposed to function. Any other solutions?? [import]uid: 23546 topic_id: 12771 reply_id: 46913[/import]

i see that line 55 initialises s1 and then line 58 again contains a definition local s1…

can you declare local s1 at the top of your function and then try initialising it and using it there after? [import]uid: 55009 topic_id: 12771 reply_id: 46919[/import]

Thanks for the reply. Could you explain it a little bit more in detail?

Thank you [import]uid: 23546 topic_id: 12771 reply_id: 46953[/import]

Anyone? [import]uid: 23546 topic_id: 12771 reply_id: 47025[/import]

If it helps here is the .zip file for this.

http://www.mediafire.com/?tnhydtr5pyrnxtn [import]uid: 23546 topic_id: 12771 reply_id: 46984[/import]

Anyone? [import]uid: 23546 topic_id: 12771 reply_id: 47074[/import]