Maurize Posted April 13, 2015 Posted April 13, 2015 Hello everybody, I have following problem: When I use a timer clientside in a function, how is it possible to delete it out of another function? I'm trying any way I know but nothing seems to work... Thanks.
Ryancit2 Posted April 13, 2015 Posted April 13, 2015 do it like this: function timedFunc(arg) -- rest of function... end timer = setTimer(timedFunc, 50, 50) function otherFunc(arg) killTimer(timer) -- It will delete timer since the timer is a global value attached with upper function. end
Maurize Posted April 13, 2015 Author Posted April 13, 2015 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.
Enargy, Posted April 14, 2015 Posted April 14, 2015 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. I did not understood, Ryancit2 code correctly work. what do you want to do exactly?
Walid Posted April 14, 2015 Posted April 14, 2015 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
Ryancit2 Posted April 14, 2015 Posted April 14, 2015 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. Just try what i and Walid wrote and let us know the results.
Maurize Posted April 16, 2015 Author Posted April 16, 2015 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 )
Enargy, Posted April 16, 2015 Posted April 16, 2015 addEvent( "...", true ) addEventHandler( "...", player, function() killTimer( xy ) -- doesnt work, gives error. expected element/timer at line xy end) Exactly where is defined xy? because I did not seen that it's defined.
Ryancit2 Posted April 16, 2015 Posted April 16, 2015 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.
Maurize Posted April 16, 2015 Author Posted April 16, 2015 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
Ryancit2 Posted April 16, 2015 Posted April 16, 2015 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.
Maurize Posted April 16, 2015 Author Posted April 16, 2015 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?
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