Skip to content
This repository was archived by the owner on May 26, 2025. It is now read-only.

Commit e2c2abc

Browse files
committed
...
1 parent 88ee09e commit e2c2abc

File tree

1 file changed

+29
-11
lines changed

1 file changed

+29
-11
lines changed

SAPP SCRIPTS/UTILITY MODS/Custom Loadouts.lua

Lines changed: 29 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -282,7 +282,7 @@ local loadouts = {
282282
-----------------------------------
283283

284284
local players
285-
local command_cooldown = {} -- Stores the cooldown state for each player
285+
local command_cooldown = {}
286286

287287
api_version = '1.12.0.0'
288288

@@ -304,7 +304,7 @@ end
304304
function OnStart()
305305
if get_var(0, '$gt') ~= 'n/a' then
306306
players = {}
307-
for i = 1,16 do
307+
for i = 1, 16 do
308308
if player_present(i) then
309309
OnJoin(i)
310310
end
@@ -365,7 +365,7 @@ end
365365

366366
function OnQuit(playerId)
367367
players[playerId] = nil
368-
command_cooldown[playerId] = nil -- Clear cooldown on player quit
368+
command_cooldown[playerId] = nil
369369
end
370370

371371
local function getLoadouts()
@@ -390,33 +390,51 @@ local function showLoadouts(playerId)
390390
end
391391
end
392392

393+
local function handleCooldown(playerId, current_time)
394+
if command_cooldown[playerId] and current_time < command_cooldown[playerId] then
395+
local remaining_time = command_cooldown[playerId] - current_time
396+
rprint(playerId, "You must wait " .. remaining_time .. " seconds before using that command again.")
397+
return true
398+
end
399+
return false
400+
end
401+
402+
local function checkAndSetCooldown(playerId)
403+
local current_time = os.time()
404+
if handleCooldown(playerId, current_time) then
405+
return false
406+
end
407+
command_cooldown[playerId] = current_time + cooldown_time
408+
return true
409+
end
410+
393411
function OnCommand(playerId, command)
394-
local command_num = tonumber(command:match("^(%d+)"))
395-
if command_num and playerId ~= 0 then
412+
if playerId == 0 then return true end
396413

397-
local current_time = os.time()
398-
if command_cooldown[playerId] and current_time < command_cooldown[playerId] then
399-
local remaining_time = command_cooldown[playerId] - current_time
400-
rprint(playerId, "You must wait " .. remaining_time .. " seconds before using that command again.")
414+
local command_num = tonumber(command:match("^(%d+)"))
415+
if command_num then
416+
if not checkAndSetCooldown(playerId) then
401417
return false
402418
end
403419

404420
for loadout_name, loadout in pairs(loadouts) do
405421
if loadout.id == command_num then
406422
players[playerId] = loadout_name
407423
rprint(playerId, "Loadout [" .. loadout_name .. "] selected! It will take effect upon respawn.")
408-
command_cooldown[playerId] = current_time + cooldown_time
409424
return false
410425
end
411426
end
412427
rprint(playerId, "Invalid loadout number!")
428+
413429
elseif command:lower() == "loadouts" then
430+
if not checkAndSetCooldown(playerId) then
431+
return false
432+
end
414433
showLoadouts(playerId)
415434
return false
416435
end
417436
end
418437

419-
420438
function OnDeath(playerId)
421439
showLoadouts(playerId)
422440
end

0 commit comments

Comments
 (0)