Jump to content

[Help] cooldown


Lachuks

Recommended Posts

Hello! Recently I made VIP panel witch works well. I wanted to add cooldown function but have no idea how to make it done. With cooldown I mean when you take VIP items you can't take them again for some time. That's my one of first scripts ever made. I hope you help me. Thanks!

Code:

function weaponsKit()
local account = getPlayerAccount ( source )   
local accName = getAccountName ( account ) 
	if isObjectInACLGroup ("user."..accName, aclGetGroup ( "VIP" ) ) then 
  		setElementData(source, "MAX_Slots", 200)
  		setElementData(source, "M4", 1)
		outputChatBox("#00ff00Weapon VIP #ffffffitems.",source, 255, 255, 255, true)
	else
		outputChatBox("#ff0000[Error] #ffffffYou don't have VIP!",source, 255, 255, 255, true)
	end
end
addEvent("onWeaponKitTake", true)
addEventHandler("onWeaponKitTake", getRootElement(), weaponsKit)

function medicalKit()
local account = getPlayerAccount ( source )   
local accName = getAccountName ( account ) 
	if isObjectInACLGroup ("user."..accName, aclGetGroup ( "VIP" ) ) then 
		setElementData(source, "Medic Kit", 2)
		setElementData(source, "Morphine", 2)
		outputChatBox("#00ff00Medical VIP #ffffffitems.",source, 255, 255, 255, true)
	else 
		outputChatBox("#ff0000[Error] #ffffffYou don't have VIP!",source, 255, 255, 255, true)
	end
end
addEvent("onMedicalKitTake", true)
addEventHandler("onMedicalKitTake", getRootElement(), medicalKit)

Link to comment

Try.

cooldowns = {}
local cooldowntime = 60000 -- Cooldown for 1 minute / 60 seconds

function removeCooldown(thePlayer)
	if cooldowns[thePlayer] then
		cooldowns[thePlayer] = nil
	end
end

function weaponsKit()
	if cooldowns[source] then
		if isTimer(cooldowns[source]) then
			local remaining = getTimerDetails(cooldowns[source])
			outputChatBox("#00ff00Cooldown remaining: ".. remaining/1000 .." seconds.",source, 255, 255, 255, true)
			return
		end
	end
	cooldowns[source] = setTimer(removeCooldown, cooldowntime, 1, source)
	local account = getPlayerAccount ( source )   
	local accName = getAccountName ( account ) 
	if isObjectInACLGroup ("user."..accName, aclGetGroup ( "VIP" ) ) then 
		setElementData(source, "MAX_Slots", 200)
		setElementData(source, "M4", 1)
		outputChatBox("#00ff00Weapon VIP #ffffffitems.",source, 255, 255, 255, true)
	else
		outputChatBox("#ff0000[Error] #ffffffYou don't have VIP!",source, 255, 255, 255, true)
	end
end
addEvent("onWeaponKitTake", true)
addEventHandler("onWeaponKitTake", getRootElement(), weaponsKit)

function medicalKit()
	if cooldowns[source] then
		if isTimer(cooldowns[source]) then
			local remaining = getTimerDetails(cooldowns[source])
			outputChatBox("#00ff00Cooldown remaining: ".. remaining/1000 .." seconds.",source, 255, 255, 255, true)
			return
		end
	end
	cooldowns[source] = setTimer(removeCooldown, cooldowntime, 1, source)
	local account = getPlayerAccount ( source )   
	local accName = getAccountName ( account ) 
	if isObjectInACLGroup ("user."..accName, aclGetGroup ( "VIP" ) ) then 
		setElementData(source, "Medic Kit", 2)
		setElementData(source, "Morphine", 2)
		outputChatBox("#00ff00Medical VIP #ffffffitems.",source, 255, 255, 255, true)
	else 
		outputChatBox("#ff0000[Error] #ffffffYou don't have VIP!",source, 255, 255, 255, true)
	end
end
addEvent("onMedicalKitTake", true)
addEventHandler("onMedicalKitTake", getRootElement(), medicalKit)

 

  • Like 1
Link to comment

@Emix Tickcount could work, but you can also just use the setTimer() func.

Are you looking for a global timer or one on a player basis? Here's two examples:

local timers = {}

function weaponsKit()
	if isTimer(timers[source]) then -- check table for player timer
		local remaining = math.ceil(getTimerDetails(timers[source])/1000)
		outputChatBox("Sorry, you can only take this item every 10 seconds! Please wait "..remaining.." more seconds")
	else
		local account = getPlayerAccount ( source )   
		local accName = getAccountName ( account ) 
		if isObjectInACLGroup ("user."..accName, aclGetGroup ( "VIP" ) ) then 
			setElementData(source, "MAX_Slots", 200)
			setElementData(source, "M4", 1)
			outputChatBox("#00ff00Weapon VIP #ffffffitems.",source, 255, 255, 255, true)
		else
			outputChatBox("#ff0000[Error] #ffffffYou don't have VIP!",source, 255, 255, 255, true)
		end
		
		timers[source] = setTimer(function() -- define timer here to table and remove it after 10 seconds
			timers[source] = nil
		end, 10000, 1) -- 10000 ms (10 secs)
	end
end
addEvent("onWeaponKitTake", true)
addEventHandler("onWeaponKitTake", getRootElement(), weaponsKit)

Global timer:

local timer;

function weaponsKit()
	if isTimer(timer) then -- check for timer
		local remaining = math.ceil(getTimerDetails(timers[source])/1000)
		outputChatBox("Sorry, you can only take this item every 10 seconds! Please wait "..remaining.." more seconds")
	else
		local account = getPlayerAccount ( source )   
		local accName = getAccountName ( account ) 
		if isObjectInACLGroup ("user."..accName, aclGetGroup ( "VIP" ) ) then 
			setElementData(source, "MAX_Slots", 200)
			setElementData(source, "M4", 1)
			outputChatBox("#00ff00Weapon VIP #ffffffitems.",source, 255, 255, 255, true)
		else
			outputChatBox("#ff0000[Error] #ffffffYou don't have VIP!",source, 255, 255, 255, true)
		end
		
		timer = setTimer(function() -- initiate timer and get rid of it after 10 secs
			timer = nil
		end, 10000, 1) -- 10000 ms (10 secs)
	end
end
addEvent("onWeaponKitTake", true)
addEventHandler("onWeaponKitTake", getRootElement(), weaponsKit)

Hope this helps.

 

EDIT: Oh my bad, didn't see your post @idarrr! Well.. you can never have too many examples :-) Pick whichever one makes the most sense to you.

Edited by eggman
  • Like 2
Link to comment

Thank you very mutch! Can you help me with one more thing?

How to fix this? I want make it look like - 5 hours 29 minutes 42 seconds not like this.

 

 

2bvtBpz.png

code:

 

local seconds = math.ceil(getTimerDetails(timers[source])/1000)
local minutes = math.ceil(getTimerDetails(timers[source])/60000)
local hours = math.ceil(getTimerDetails(timers[source])/3600000)
outputChatBox("Sorry, you can only take this item every 5 hours! Please wait "..hours.." Hours"..minutes.." Minutes"..seconds.." Seconds")
Edited by Emix
Link to comment
53 minutes ago, eggman said:

This may be what you're looking for


local secs = getTimerDetails(timers[source])/1000
local remaining = string.format("%.1d hours %.1d minutes and %.1d seconds", secs/(60*60), secs/60%60, secs%60)

 

Thank you! :*

  • Like 1
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...