Jump to content

Bad argument @ 'setTimer'


dragonofdark

Recommended Posts

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
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

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

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
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

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 account

Sign in

Already have an account? Sign in here.

Sign In Now
  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...