Jump to content

killTimer is not working.


Emanuel

Recommended Posts

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
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 by Ayush Rathore
Link to comment

I will try to explain my idea using a scheme.

  1. A person commits a crime that gives a wanted level of 5.
  2. Start timer to decrease his wanted level to 0.
  3. 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.)
  4. In each time step, wanted level = wanted level - 1
  5. When the wanted level becomes 0, destroy the timer.
Edited by Emanuel
Link to comment
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

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