Emanuel Posted February 26, 2017 Share Posted February 26, 2017 Good day! This is my code where the objective is to set the player wanted level. After increasing the wanted level, I want to create a timer in order to start decreasing it. The problem is that in my code, at each wanted level increase, it creates a timer, which is pretty annoying. The thing is, how can I check if there is a timer and destroy it, before starting the new one? I thought I did that, but the code is not working! function setWantedLevel(player) local wantedLevel = getElementData(player,"wantedLevel") if wantedLevel == 10 then setElementData(player,"wantedLevel",10,true) exports.SASCnotifications:drawNotification("#FF0000[Wanted Level System]#FFFFFF Wanted level is "..wantedLevel..".",localPlayer) else setElementData(player,"wantedLevel",wantedLevel+1,true) exports.SASCnotifications:drawNotification("#FF0000[Wanted Level System]#FFFFFF Wanted level increased to "..(wantedLevel+1)..".",localPlayer) if getElementData(player,"wantedLevel") == 3 then triggerServerEvent("setCriminalJob",player,player) end end timer = nil timer = setTimer(function(myTimer) if isTimer(myTimer) then killTimer(myTimer) timer=nil end local wantedLevel = getElementData(player,"wantedLevel") if wantedLevel == 0 then killTimer(myTimer) elseif wantedLevel ~= 0 then setElementData(player,"wantedLevel",wantedLevel-1,true) exports.SASCnotifications:drawNotification("#FF0000[Wanted Level System]#FFFFFF Wanted level decreased to "..(wantedLevel-1)..".",player) end end,5000,0,timer) end Link to comment
Ayush Rathore Posted February 26, 2017 Share Posted February 26, 2017 (edited) local timer = {} function setWantedLevel(player) local wantedLevel = getElementData(player,"wantedLevel") if wantedLevel == 10 then setElementData(player,"wantedLevel",10,true) exports.SASCnotifications:drawNotification("#FF0000[Wanted Level System]#FFFFFF Wanted level is "..wantedLevel..".",localPlayer) else setElementData(player,"wantedLevel",wantedLevel+1,true) exports.SASCnotifications:drawNotification("#FF0000[Wanted Level System]#FFFFFF Wanted level increased to "..(wantedLevel+1)..".",localPlayer) if getElementData(player,"wantedLevel") == 3 then triggerServerEvent("setCriminalJob",player,player) end end if timer[player] == false then timer[player] = setTimer(function() local wantedLevel = getElementData(player,"wantedLevel") if wantedLevel == 0 then killTimer(timer[player]) timer[player] = false else setElementData(player,"wantedLevel",wantedLevel-1,true) exports.SASCnotifications:drawNotification("#FF0000[Wanted Level System]#FFFFFF Wanted level decreased to "..(wantedLevel-1)..".",player) end end,5000,0) end end function load() timer[source] = false end addEventHandler("onPlayerJoin", getRootElement(), load) Try this when you will run this it will decrease wanted level by 1 every 5 sec till it reaches 0 and will not make it infinite Edited February 26, 2017 by Ayush Rathore Link to comment
Emanuel Posted February 26, 2017 Author Share Posted February 26, 2017 It didn't work. It still creates a timer each time I call the function, but doesn't destroy the previous one. Link to comment
Ayush Rathore Posted February 26, 2017 Share Posted February 26, 2017 well why you want the timer Link to comment
Emanuel Posted February 26, 2017 Author Share Posted February 26, 2017 (edited) I will try to explain my idea using a scheme. A person commits a crime that gives a wanted level of 5. Start timer to decrease his wanted level to 0. If there is a timer, destroy it, and start a new one. (Here is the problem. Since it is creating multiple ones, the wanted level goes to zero quickly because each timer is decreasing it.) In each time step, wanted level = wanted level - 1 When the wanted level becomes 0, destroy the timer. Edited February 26, 2017 by Emanuel Link to comment
Ayush Rathore Posted February 26, 2017 Share Posted February 26, 2017 local timer = {} function setWantedLevel(player) local wantedLevel = getElementData(player,"wantedLevel") if wantedLevel == 10 then setElementData(player,"wantedLevel",10,true) exports.SASCnotifications:drawNotification("#FF0000[Wanted Level System]#FFFFFF Wanted level is "..wantedLevel..".",localPlayer) else setElementData(player,"wantedLevel",wantedLevel+1,true) exports.SASCnotifications:drawNotification("#FF0000[Wanted Level System]#FFFFFF Wanted level increased to "..(wantedLevel+1)..".",localPlayer) if getElementData(player,"wantedLevel") == 3 then triggerServerEvent("setCriminalJob",player,player) end end if timer[player] == false then timer[player] = setTimer(function() local wantedLevel = getElementData(player,"wantedLevel") if wantedLevel == 0 then killTimer(timer[player]) timer[player] = false else setElementData(player,"wantedLevel",wantedLevel-1,true) exports.SASCnotifications:drawNotification("#FF0000[Wanted Level System]#FFFFFF Wanted level decreased to "..(wantedLevel-1)..".",player) end end,5000,0) else killTimer(timer[player]) timer[player] = false setWantedLevel(player) end end function load() timer[source] = false end addEventHandler("onPlayerJoin", getRootElement(), load) Try This Link to comment
Emanuel Posted February 26, 2017 Author Share Posted February 26, 2017 It is working, at least the decreasing part is. The bug now is that the wanted level goes up by 2, but I think I can fix it now. Thanks Ayush! Link to comment
Ayush Rathore Posted February 26, 2017 Share Posted February 26, 2017 (edited) Your Welcome Edited February 26, 2017 by Ayush Rathore 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