dragonofdark Posted February 1, 2012 Share Posted February 1, 2012 Hey, I'm making some scripts but I'm blocked at one. I make a function to create 2 lights which flash randomly. Here the code part which I have an error : local random1 = math.random(50,300) local random2 = math.random(50,300) setTimer( flash1, random1, 0 ) setTimer( flash2, random2, 0 ) I have this error for the 2 "setTimer": Bad argument @ 'setTimer' [Expected function at argument 1, got nil] Where did I make a mistake ? Thanks. Link to comment
Castillo Posted February 1, 2012 Share Posted February 1, 2012 That error means the function "flash2" is nil, post the rest of the script. Link to comment
dragonofdark Posted February 1, 2012 Author Share Posted February 1, 2012 function startLights( player, cmd ) local vehicle = getPedOccupiedVehicle(player) local x,y,z = getElementPosition( vehicle ) obj1 = createObject(2710,x,y,z) attachElements(obj1,vehicle,0.5,0.7,1.19) obj2 = createObject(2710,x,y,z) attachElements(obj2,vehicle,-0.5,0.7,1.19) lit1 = createMarker(x,y,z,"corona",0.25,0,0,255,0) attachElements(lit1, obj1, 0, 0, 0.1) lit2 = createMarker(x,y,z,"corona",0.25,0,0,255,0) attachElements(lit2, obj2, 0, 0, 0.1) local random1 = math.random(50,300) local random2 = math.random(50,300) setTimer( flash1, random1, 0 ) setTimer( flash2, random2, 0 ) function flash1() setMarkerColor(lit1, 0, 0, 255, 200) setTimer( setMarkerColor, 50, 1, lit1, 0, 0, 0, 0) end function flash2() setMarkerColor(lit2, 0, 0, 255, 200) setTimer( setMarkerColor, 50, 1, lit2, 0, 0, 0, 0) end end addCommandHandler ( "light", startLights) Link to comment
Castillo Posted February 1, 2012 Share Posted February 1, 2012 Try this: local markers = {} function startLights( player, cmd ) if (not markers[player]) then markers[player] = {} end local vehicle = getPedOccupiedVehicle(player) local x,y,z = getElementPosition( vehicle ) obj1 = createObject(2710,x,y,z) attachElements(obj1,vehicle,0.5,0.7,1.19) obj2 = createObject(2710,x,y,z) attachElements(obj2,vehicle,-0.5,0.7,1.19) markers[player][1] = createMarker(x,y,z,"corona",0.25,0,0,255,0) attachElements(markers[player][1], obj1, 0, 0, 0.1) markers[player][2] = createMarker(x,y,z,"corona",0.25,0,0,255,0) attachElements(markers[player][1], obj2, 0, 0, 0.1) local random1 = math.random(50,300) local random2 = math.random(50,300) setTimer( flash1, random1, 0, player ) setTimer( flash2, random2, 0, player ) end addCommandHandler ( "light", startLights) function flash1(player) setMarkerColor(markers[player][1], 0, 0, 255, 200) setTimer( setMarkerColor, 50, 1, markers[player][1], 0, 0, 0, 0) end function flash2(player) setMarkerColor(markers[player][2], 0, 0, 255, 200) setTimer( setMarkerColor, 50, 1, markers[player][2], 0, 0, 0, 0) end Link to comment
dragonofdark Posted February 1, 2012 Author Share Posted February 1, 2012 Works perfectly ! Did the flash functions inside the main funtion caused the problem ? Link to comment
Castillo Posted February 1, 2012 Share Posted February 1, 2012 Yeah, also, every time someone write that command another two different markers would be created with the same variable name, that's why I used a table. Link to comment
dragonofdark Posted February 1, 2012 Author Share Posted February 1, 2012 Okay, will remember this tip Link to comment
dragonofdark Posted February 1, 2012 Author Share Posted February 1, 2012 Sorry for the double posting, but I found another problem. When someone do the command when the lights are already effective, the old lights stop to flash and the new have 2 "setTimer" (the old and the new). Is that problem related to these tables ? It should normaly keep the old flashing and create others without interfering, no ? Link to comment
Castillo Posted February 1, 2012 Share Posted February 1, 2012 local markers = {} local timers = {} function startLights( player, cmd ) if (not markers[player]) then markers[player] = {} end if (not timers[player]) then timers[player] = {} end if isTimer(timers[player][1]) then killTimer(timers[player][1]) end if isTimer(timers[player][2]) then killTimer(timers[player][2]) end if isElement(markers[player][1]) then destroyElement(markers[player][1]) end if isElement(markers[player][2]) then destroyElement(markers[player][2]) end local vehicle = getPedOccupiedVehicle(player) local x,y,z = getElementPosition( vehicle ) obj1 = createObject(2710,x,y,z) attachElements(obj1,vehicle,0.5,0.7,1.19) obj2 = createObject(2710,x,y,z) attachElements(obj2,vehicle,-0.5,0.7,1.19) markers[player][1] = createMarker(x,y,z,"corona",0.25,0,0,255,0) attachElements(markers[player][1], obj1, 0, 0, 0.1) markers[player][2] = createMarker(x,y,z,"corona",0.25,0,0,255,0) attachElements(markers[player][1], obj2, 0, 0, 0.1) local random1 = math.random(50,300) local random2 = math.random(50,300) timers[player][1] = setTimer( flash1, random1, 0, player ) timers[player][2] = setTimer( flash2, random2, 0, player ) end addCommandHandler ( "light", startLights) function flash1(player) setMarkerColor(markers[player][1], 0, 0, 255, 200) setTimer( setMarkerColor, 50, 1, markers[player][1], 0, 0, 0, 0) end function flash2(player) setMarkerColor(markers[player][2], 0, 0, 255, 200) setTimer( setMarkerColor, 50, 1, markers[player][2], 0, 0, 0, 0) end Link to comment
dragonofdark Posted February 1, 2012 Author Share Posted February 1, 2012 Thank you, castillo Everything works perfectly now. Link to comment
Castillo Posted February 1, 2012 Share Posted February 1, 2012 You're welcome . P.S: I would add to kill, destroy markers on quit. Link to comment
dragonofdark Posted February 1, 2012 Author Share Posted February 1, 2012 P.S: I would add to kill, destroy markers on quit. Will add it Link to comment
Recommended Posts
Create an account or sign in to comment
You need to be a member in order to leave a comment
Create an account
Sign up for a new account in our community. It's easy!
Register a new accountSign in
Already have an account? Sign in here.
Sign In Now