Skip to content
swkeep edited this page Jul 2, 2022 · 2 revisions

Welcome to the keep-cooldown wiki!

a brief tutorial on how to use the script:

Types

  • there are two types of cooldown. temporary or persistent.
  • temporary cooldowns are only available during runtime.
  • persistent cooldowns their state will be saved to the database and loaded back after the server started.

How to use script?

local CD = exports["keep-cooldown"]:CD()

How to create a new cooldown?

  • you can specify a predefined id. if you don't define the id script will return a random Id.
  • cooldownLength is required and determines the length of the cooldown. CD.setOnCooldown(ID) will set timer to this value.
  • cType can be either 'temporary' or 'persistent'.
local cd_id =
    CD.add(
    {
        id = "cooldown_1",
        cooldownLength = 30,
        cType = "temporary"
    }
)

How to check cooldown's state or remaining time?

  • you can check cooldown state and remaining by calling CD.isOnCooldown(ID).
  • it will return state and remaining time but if there is an error it will return nil, nil.
local state, remaining = CD.isOnCooldown(cd_id)
if state then
    print("Still on cooldown remaining: " .. remaining)
    return
end

How to set on cooldown?

  • it will return true after successful execution and return false if the id was invalid or the function failed.
CD.setOnCooldown(cd_id)

How to remove a cooldown on runtime?

  • it will return true after successful execution and return false if the id was invalid or the function failed.
CD.remove(cd_id)

How to reset the cooldown usage?

  • cooldown usage counts how many times CD.setOnCooldown() called for a cooldown.
  • doing this will set cooldown usage to 0.
CD.resetCooldownUsage(id)

How to count how many active cooldowns we have?

  • countActiveCooldowns() is only available serve-side.
CD.countActiveCooldowns()

cooldown state change events

  • there are two events that are triggered by changes in the state of cooldowns.
  • keep-cooldown:server:cooldownStateChange and keep-cooldown:client:cooldownStateChange
  • the first arg is state and the second one is Id of cooldown
-- client-side-event
RegisterNetEvent('keep-cooldown:client:cooldownStateChange', function(CD, ID)
     print(CD, ID)
end)
-- server-side-event
AddEventHandler('keep-cooldown:server:cooldownStateChange', function(CD, ID)
     print(CD, ID)
end)

Examples

  • client-side
local CD = exports["keep-cooldown"]:CD()

local cd_id =
    CD.add(
    {
        id = "keep_blackmarket_payphone" .. PlayerPedId(),
        cooldownLength = 30,
        cType = "temporary" -- or persistent
    }
)

local state, remaining = CD.isOnCooldown(cd_id)

if state then
    print("on CD " .. remaining)
    return
end

if not state then
    CD.setOnCooldown(cd_id)
end
  • server-side
local CD = exports["keep-cooldown"]:CD()

local cd_id =
    CD.add(
    {
        id = "keep_blackmarket_payphone_2",
        cooldownLength = 30,
        cType = "persistent" -- or persistent
    }
)

local state, remaining = CD.isOnCooldown(cd_id)
print(state, remaining)
if state then
    print("on CD " .. remaining)
    return
end

if not state then
    CD.setOnCooldown(cd_id)
end

state, remaining = CD.isOnCooldown(cd_id)
print(state, remaining)