tm.cooldowns
Time based cooldowns keyed by an id you choose (a character id, a citizen id, any string or number). The server owns them. The client side asks the server. For cooldowns keyed to a player's server id, see tm.players.
- One active cooldown per id: adding the same
typeagain is ignored, a differenttypereplaces it. - Everything lives in memory for the calling resource, so a restart clears it. No database is used.
- Durations are in seconds. The default is 30 minutes.
tm.cooldowns.add({ cid = id, type = 'rob_store', duration = 600 }) -- true when started, false when ignored
tm.cooldowns.get(id) -- { type, duration, startTime } or nil (also accepts { cid = id })
tm.cooldowns.timeLeft(id, 'min') -- nil when none, otherwise the time left
tm.cooldowns.remove(id) -- true when one was removed| Function | Notes |
|---|---|
add({ cid, type, duration? }) | Starts a cooldown. Returns false for bad data or when the same type is already running for that id. |
get(cid | { cid }) | A copy of the active entry (type, duration in seconds, startTime as os.time()), or nil. |
timeLeft(cid | { cid }, format?) | Seconds rounded up by default. 'sec'/'seconds', 'min'/'minutes', 'h'/'hours' and 'd'/'days' (rounded down). nil when there is no cooldown. |
remove(cid | { cid }) | Clears it early. |
setClientGuard(fn?) | Server only, see below. |
Client side
The client has the same four functions (add, get, timeLeft, remove). Each one asks the server through tm.callback and blocks the calling thread until it answers, so call them from a thread. The callback names include the resource name (tm:cooldowns:<resource>:add and so on), so two resources that use this import never answer each other's calls.
Clients cannot start or clear a cooldown by default. Without a guard only get and timeLeft are allowed, because a client must never be able to clear its own cooldown. To allow more, install a guard on the server:
tm.cooldowns.setClientGuard(function(source, action, cid)
-- action is 'add', 'get', 'timeLeft' or 'remove'; cid is the id the client named
return action ~= 'remove' and ownsCharacter(source, cid) -- your own rule, for example through the bridge
end)Return true to allow. A guard that errors denies and logs. setClientGuard(nil) returns to read only.
add returns a boolean. A replaced cooldown's old timer never clears the new one.