@@ -282,7 +282,7 @@ local loadouts = {
282
282
---- -------------------------------
283
283
284
284
local players
285
- local command_cooldown = {} -- Stores the cooldown state for each player
285
+ local command_cooldown = {}
286
286
287
287
api_version = ' 1.12.0.0'
288
288
304
304
function OnStart ()
305
305
if get_var (0 , ' $gt' ) ~= ' n/a' then
306
306
players = {}
307
- for i = 1 ,16 do
307
+ for i = 1 , 16 do
308
308
if player_present (i ) then
309
309
OnJoin (i )
310
310
end
365
365
366
366
function OnQuit (playerId )
367
367
players [playerId ] = nil
368
- command_cooldown [playerId ] = nil -- Clear cooldown on player quit
368
+ command_cooldown [playerId ] = nil
369
369
end
370
370
371
371
local function getLoadouts ()
@@ -390,33 +390,51 @@ local function showLoadouts(playerId)
390
390
end
391
391
end
392
392
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
+
393
411
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
396
413
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
401
417
return false
402
418
end
403
419
404
420
for loadout_name , loadout in pairs (loadouts ) do
405
421
if loadout .id == command_num then
406
422
players [playerId ] = loadout_name
407
423
rprint (playerId , " Loadout [" .. loadout_name .. " ] selected! It will take effect upon respawn." )
408
- command_cooldown [playerId ] = current_time + cooldown_time
409
424
return false
410
425
end
411
426
end
412
427
rprint (playerId , " Invalid loadout number!" )
428
+
413
429
elseif command :lower () == " loadouts" then
430
+ if not checkAndSetCooldown (playerId ) then
431
+ return false
432
+ end
414
433
showLoadouts (playerId )
415
434
return false
416
435
end
417
436
end
418
437
419
-
420
438
function OnDeath (playerId )
421
439
showLoadouts (playerId )
422
440
end
0 commit comments