Jump to content

Functions and Timers


Maurize

Recommended Posts

This isn't possible.

For example:

Function got triggered by Server and starts a timer.

Timer is working.

1 Minute later I need to kill Timer.

Ryan code is correct just if you want to trigger the function from the server side , so you need to add the timer inside the function .

function timedFunc(arg) 
    -- rest of function... 
       timer = setTimer(timedFunc,60000, 0) -- 1 munite 
end 
addEvent("checkTimer",true) 
addEventHandler("checkTimer",root,timedFunc) 
  
-- Function to kill the timer 
function otherFunc(arg) 
    if isTimer (timer) then  
    killTimer(timer) -- It will delete timer since the timer is a global value attached with upper function. 
    end 
end 

Link to comment

I know your code is right but that is not the point.

The point is, that I need to kill these timers in a new function...

addEvent( "onPedIntro1", true ) 
addEventHandler( "onPedIntro1", player, 
function( ped ) 
    gangster_timer = setTimer( 
function() 
if ( ped ) then 
    setPedAnalogControlState( ped, "special_control_left", math.random( 0, 1 ) ); 
    setPedAnalogControlState( ped, "special_control_right", math.random( 0, 1 ) ); 
    setPedAnalogControlState( ped, "special_control_down", math.random( 0, 1 ) ); 
    setPedAnalogControlState( ped, "special_control_up", math.random( 0, 1 ) ); 
        end 
    end, math.random( 500, 900 ), 0 ) 
end ) 
  
addEvent( "onPedIntro2", true ) 
addEventHandler( "onPedIntro2", player, 
function( ped ) 
    police_timer = setTimer( 
function() 
if ( ped ) then 
    local pX, pY, pZ = getPedBonePosition( player, math.random( 1, 54 ) ); 
    setPedControlState( ped, "fire", true ); 
    setPedAimTarget( ped, pX, pY, pZ ); 
        end 
    end, math.random( 500, 1000 ), 0 ) 
end ) 
  
addEvent( "...", true ) 
addEventHandler( "...", player, 
function() 
killTimer( xy ) -- doesnt work, gives error. expected element/timer at line xy 
end) 

SECOND is, that I need to check if 1 hour is reached...

Players time is stored in element data by seconds.

Now I have to check if a hour is passed ( because payday and stuff ).

Any ideas?

  
addEventHandler( "onClientResourceStart", resourceRoot, 
function() 
    setTimer( 
function() 
if ( getElementData( element, "time" ) / 3600 == 1 ) then -- yeah, 1 hour is passed but how to get next? 
    triggerServerEvent( "onPlayerTax", element ); 
    end, 1000, 0 ) 
end ) 
  

Link to comment

I'd prefer not to call elementData so often, instead make tables as acc names = keys and times as values of accounts, then update table on regular intervals and then check whether the time has reached or not, something like this:

  
timeCache = { 
--["acc"] = time, 
} 
  
function updateTime(acc) 
    timer = setTimer(function(acc) timeCache[acc] = timeCache[acc] + 5, 5000, 0) -- Will update after every 5 seconds, though timer won't be killed, no need. 
end 
  
-- Now call updateTimer(acc) function in other functions but before that, make a check like this: 
if timeCache[acc] >= 3600 then timeCache[acc] = 0 return end 
  

Though this just as idea, showing how you should use tables to handle timers.

Link to comment
Ryancit2, this is a good solution. I could insert all timers in table, make a for slope aand kill every timer! Thanks for giving me that idea :)

By the way its for loop.

Also, you can add timers under acc name key aswell, like this:

accTimers = { 
["account"] = setTimer( ... ) 
} 

And then you can loop through table.

Link to comment
addEvent( "onPlayerTax", true ); 
addEventHandler( "onPlayerTax", element, 
function() 
    delay = 3600 - getElementData( element, "time" ); -- value starts from 1 and gets every second +1. Is loaded from database at spawn and saved in ElementData. 
    setTimer( 
function() 
    outputChatBox( "PAYDAY!" ); 
    delay = 3600; 
    end, delay, 0 ) 
end ) 

But what if player time is more than 3600? for example : delay = 3600 - 120120.

Is there a math calculation which gets if a full hour is reached?

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