Author Topic: cooldown period on a command  (Read 1823 times)

0 Members and 1 Guest are viewing this topic.

Offline JasonMan

  • Newbie
  • *
  • Posts: 12
  • Karma: -1
cooldown period on a command
« on: July 28, 2015, 08:42:00 AM »
I modified this ulx unstuck for prophunt:
Code: [Select]
function ulx.unstuck( calling_ply )

if not calling_ply:Alive() and calling_ply:IsValid() then
ULib.tsayColor( calling_ply, _, Color( 255, 0, 0 ), "You must be alive to use this command!" )
else
if CurTime() - ( lasttime or 0 ) < 15 then
local cooldowntime = 15 - CurTime() + lasttime
ULib.tsayColor( calling_ply, _, Color( 255, 255, 255 ), "You must wait ", Color( 50, 50, 50 ), tostring( math.floor( cooldowntime ) ) , Color( 255, 255, 255 ), " more seconds before using this command again" )
else
if calling_ply:Team() == TEAM_HUNTERS then
smgnadeammo = calling_ply:GetAmmoCount( 9 )
end
lasttime = CurTime()
if calling_ply:IsValid() then
calling_ply:Spawn()
if calling_ply:Team() == TEAM_HUNTERS then
timer.Simple( 0.3, function()
calling_ply:SetAmmo( smgnadeammo, 9 )
end )
end
end
ULib.tsayColor( nil, _, Color( 255, 255, 255 ), calling_ply:Nick(), Color(0, 0, 255), " has been unstuck!" )
end
end
end
local unstuck = ulx.command( CATEGORY_NAME, "ulx unstuck", ulx.unstuck, "!unstuck" )
unstuck:defaultAccess( ULib.ACCESS_ALL )
unstuck:help( "Respawn yourself. There's a 15 seconds cooldown" )

Problem is: If one player uses the command, the cooldown period starts for all players on the server

Offline MrPresident

  • Ulysses Team Member
  • Hero Member
  • *****
  • Posts: 2728
  • Karma: 430
    • |G4P| Gman4President
Re: cooldown period on a command
« Reply #1 on: July 28, 2015, 10:42:10 AM »
That's because you're setting lasttime as a global.
You need to have lasttime be specific for the player.

do calling_ply.lasttime

then first line after your else, do

if not calling_ply.lasttime then calling_ply.lasttime = CurTime() - 20 end

Offline JasonMan

  • Newbie
  • *
  • Posts: 12
  • Karma: -1
Re: cooldown period on a command
« Reply #2 on: July 28, 2015, 11:28:38 AM »
That's because you're setting lasttime as a global.
You need to have lasttime be specific for the player.

do calling_ply.lasttime

then first line after your else, do

if not calling_ply.lasttime then calling_ply.lasttime = CurTime() - 20 end

Ye I knew I was setting it as a global but I didn't think about doing that, thanks :D

Also about that "if not calling_ply.lasttime then calling_ply.lasttime = CurTime() - 20 end"
I had that (if not ... lasttime = 0 ) but I thought that lasttime or 0 was cleaner.
Any specific reason on why do it that way?

Offline MrPresident

  • Ulysses Team Member
  • Hero Member
  • *****
  • Posts: 2728
  • Karma: 430
    • |G4P| Gman4President
Re: cooldown period on a command
« Reply #3 on: July 29, 2015, 04:47:44 PM »
Either way works. I just prefer to set the variable if it's nil manually rather than writing it into any logic that might need it in the function.