Jump to content

Timer


Recommended Posts

Hello guys, the timer is not working very well. I think he is working just one time, when the player go into the server. What i did wrong?

			elseif itemData["id"] == 34 then
				local emveiculo = getPedOccupiedVehicle ( localPlayer )
				local outskin = tonumber(getElementData(localPlayer, "skinout"))
				if not emveiculo then
					if getElementData(localPlayer, "Male") == true then
						if getElementData(localPlayer, "WearingSkin") == false then
						setTimer ( function(localPlayer)
							skinatual = getElementModel(localPlayer)
							setElementData(localPlayer, "skinout", skinatual)
							setElementData(localPlayer, "WearingSkin", true)
							triggerServerEvent("itemAnims",localPlayer,localPlayer,34)
						end, 10000, 0 )
						else
						setTimer ( function(localPlayer)
							triggerServerEvent("itemAnims",localPlayer,localPlayer,400)
						end, 10000, 0 )
						end
				
					else
						exports.YM_DxMessages:addBox("Male Stuff!", "error")
					end
				else
					exports.YM_DxMessages:addBox("U cant do that!", "error")
				end

 

Edited by GodKraken
Link to post

I think you create a spiral.

			elseif itemData["id"] == 34 then
				local emveiculo = getPedOccupiedVehicle ( localPlayer )
				local outskin = tonumber(getElementData(localPlayer, "skinout"))
				if not emveiculo then
					if getElementData(localPlayer, "Male") == true then
						if getElementData(localPlayer, "WearingSkin") == false then
						setTimer ( function(localPlayer)
							skinatual = getElementModel(localPlayer)
							setElementData(localPlayer, "skinout", skinatual)
							setElementData(localPlayer, "WearingSkin", true)
							triggerServerEvent("itemAnims",localPlayer,localPlayer,34)
						end, 10000, 1 )
						else
						setTimer ( function(localPlayer)
							triggerServerEvent("itemAnims",localPlayer,localPlayer,400)
						end, 10000, 1 )
						end
				
					else
						exports.YM_DxMessages:addBox("Male Stuff!", "error")
					end
				else
					exports.YM_DxMessages:addBox("U cant do that!", "error")
				end

 

  • Like 1
Link to post
36 minutes ago, RekZ said:

I think you create a spiral.


			elseif itemData["id"] == 34 then
				local emveiculo = getPedOccupiedVehicle ( localPlayer )
				local outskin = tonumber(getElementData(localPlayer, "skinout"))
				if not emveiculo then
					if getElementData(localPlayer, "Male") == true then
						if getElementData(localPlayer, "WearingSkin") == false then
						setTimer ( function(localPlayer)
							skinatual = getElementModel(localPlayer)
							setElementData(localPlayer, "skinout", skinatual)
							setElementData(localPlayer, "WearingSkin", true)
							triggerServerEvent("itemAnims",localPlayer,localPlayer,34)
						end, 10000, 1 )
						else
						setTimer ( function(localPlayer)
							triggerServerEvent("itemAnims",localPlayer,localPlayer,400)
						end, 10000, 1 )
						end
				
					else
						exports.YM_DxMessages:addBox("Male Stuff!", "error")
					end
				else
					exports.YM_DxMessages:addBox("U cant do that!", "error")
				end

 

What i want is block this elseif itemData["id"] == 34 then for 10 seconds, but is not working :/

Link to post
  • Moderators

I recommend to check/remove the following:

setTimer ( function(localPlayer)

Overwriting the localPlayer with a nil value should cause problems.

 

 

 

13 hours ago, GodKraken said:

What i want is block this elseif itemData["id"] == 34 then for 10 seconds, but is not working :/

 

 

You could use the following useful function for blocking:

elseif itemData["id"] == 34 then
	if checkPassiveTimer ( "limit-item-id-34", true, 10000 ) then -- this code can only run every 10 seconds

	end

Get the function from here:

https://wiki.multitheftauto.com/wiki/CheckPassiveTimer

  • Like 1
Link to post
10 hours ago, IIYAMA said:

I recommend to check/remove the following:

setTimer ( function(localPlayer)

Overwriting the localPlayer with a nil value should cause problems.

 

 

 

 

 

You could use the following useful function for blocking:


elseif itemData["id"] == 34 then
	if checkPassiveTimer ( "limit-item-id-34", true, 10000 ) then -- this code can only run every 10 seconds

	end

Get the function from here:

https://wiki.multitheftauto.com/wiki/CheckPassiveTimer

Is giving me a nil value 

Link to post
  • Moderators
1 hour ago, GodKraken said:

Is giving me a nil value 

The variable checkPassiveTimer? You have to add the function first before you can use it.

 

Link to post
37 minutes ago, IIYAMA said:

A variável checkPassiveTimer? Você precisa adicionar a função antes de usá-la.

 

elseif itemData["id"] == 34 then
				if checkPassiveTimer( "limit-item-id-34", true, 10000 ) then
					local emveiculo = getPedOccupiedVehicle ( localPlayer )
					local outskin = tonumber(getElementData(localPlayer, "skinout"))
					if not emveiculo then
						if getElementData(localPlayer, "Male") == true then
							if getElementData(localPlayer, "WearingSkin") == false then
								skinatual = getElementModel(localPlayer)
								setElementData(localPlayer, "skinout", skinatual)
								setElementData(localPlayer, "WearingSkin", true)
								triggerServerEvent("itemAnims",localPlayer,localPlayer,34)
							else
								triggerServerEvent("itemAnims",localPlayer,localPlayer,400)
							end
					
						else
							exports.YM_DxMessages:addBox("Male stuff!", "error")
						end
					else
						exports.YM_DxMessages:addBox("U cant do that!", "error")
					end
				else
					exports.YM_DxMessages:addBox("Wait 10 secs!", "warning")
				end

 

Link to post
  • Moderators
11 hours ago, GodKraken said:

elseif itemData["id"] == 34 then
				if checkPassiveTimer( "limit-item-id-34", true, 10000 ) then
					local emveiculo = getPedOccupiedVehicle ( localPlayer )
					local outskin = tonumber(getElementData(localPlayer, "skinout"))
					if not emveiculo then
						if getElementData(localPlayer, "Male") == true then
							if getElementData(localPlayer, "WearingSkin") == false then
								skinatual = getElementModel(localPlayer)
								setElementData(localPlayer, "skinout", skinatual)
								setElementData(localPlayer, "WearingSkin", true)
								triggerServerEvent("itemAnims",localPlayer,localPlayer,34)
							else
								triggerServerEvent("itemAnims",localPlayer,localPlayer,400)
							end
					
						else
							exports.YM_DxMessages:addBox("Male stuff!", "error")
						end
					else
						exports.YM_DxMessages:addBox("U cant do that!", "error")
					end
				else
					exports.YM_DxMessages:addBox("Wait 10 secs!", "warning")
				end

 

The checkPassiveTimer from the wiki page:

https://wiki.multitheftauto.com/wiki/CheckPassiveTimer

do
	local passiveTimerGroups = {}
	local cleanUpInterval = 240000
	local nextCleanUpCycle = getTickCount() + cleanUpInterval
	
	local onElementDestroyEventName = triggerServerEvent and "onClientElementDestroy" or "onElementDestroy"
	
	local function isEventHandlerAdded( eventName, elementAttachedTo, func ) -- https://wiki.multitheftauto.com/wiki/GetEventHandlers
		local attachedFunctions = getEventHandlers( eventName, elementAttachedTo )
		if #attachedFunctions > 0 then
			for i=1, #attachedFunctions do
				if attachedFunctions[i] == func then
					return true
				end
			end
		end
		return false
	end
	--[[
		Remove passive timers of elements that are destroyed
	]]
	local function removeDeletedElementTimer ()
		for timerName, passiveTimers in pairs(passiveTimerGroups) do
			if passiveTimers[this] then
				passiveTimers[this] = nil
				if not next(passiveTimers) then
					passiveTimerGroups[timerName] = nil
				end
			end
		end
		removeEventHandler(onElementDestroyEventName, this, removeDeletedElementTimer)
	end
	
	--[[
		Make a clean up cycle to prevent a memory leak
	]]
	local function checkCleanUpCycle (timeNow)
		if timeNow > nextCleanUpCycle then
			nextCleanUpCycle = timeNow + cleanUpInterval
			local maxExecutionTime = timeNow + 3
			for timerName, passiveTimers in pairs(passiveTimerGroups) do
				for key, executionTime in pairs(passiveTimers) do
					if timeNow > executionTime then
						if isElement(key) and isEventHandlerAdded(onElementDestroyEventName, key, removeDeletedElementTimer) then
							removeEventHandler(onElementDestroyEventName, key, removeDeletedElementTimer)
						end
						passiveTimers[key] = nil
					end
				end
				if not next(passiveTimers) then
					passiveTimerGroups[timerName] = nil
				end
				--[[
					Just making sure that during the clean-up cycle no lag spike occur.
				]]
				if getTickCount() >= maxExecutionTime then
					break
				end
			end
		end
	end
	
	function checkPassiveTimer (timerName, key, timeInterval)
		if type(timerName) ~= "string" then
			error("bad argument @ 'checkPassiveTimer' [Expected string at argument 1, got " .. type(timerName) .. "]", 2)
		elseif key == nil then
			error("bad argument @ 'checkPassiveTimer' [Expected anything except for nil at argument 2, got nil]", 2)
		end
		
		local intervalType = type(timeInterval)
		if intervalType == "string" then
			timeInterval = tonumber(timeInterval)
			if not timeInterval then
				error("bad argument @ 'checkPassiveTimer' [Expected a convertible string at argument 3]", 2)
			end
		elseif intervalType ~= "number" then
			error("bad argument @ 'checkPassiveTimer' [Expected number at argument 3, got " .. type(timeInterval) .. "]", 2)
		end
		
		--[[
			Set-up the timer
		]]
		local passiveTimers = passiveTimerGroups[timerName]
		if not passiveTimers then
			passiveTimers = {}
			passiveTimerGroups[timerName] = passiveTimers
		end
		
		local timeNow = getTickCount()


		local executionTime = passiveTimers[key]
		if executionTime then
			if timeNow > executionTime then
				passiveTimers[key] = timeNow + timeInterval
				checkCleanUpCycle(timeNow)
				return true, 0
			end
			checkCleanUpCycle(timeNow)
			return false, executionTime - timeNow
		end
		
		if isElement(key) and not isEventHandlerAdded(onElementDestroyEventName, key, removeDeletedElementTimer) then
			addEventHandler(onElementDestroyEventName, key, removeDeletedElementTimer, false, "high")
		end
		
		passiveTimers[key] = timeNow + timeInterval
		
		checkCleanUpCycle(timeNow)
		return true, 0
	end
end

 

 

 

  • Thanks 1
Link to post

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