General > Developers Corner
cooldown period on a command
(1/1)
JasonMan:
I modified this ulx unstuck for prophunt:
--- Code: ---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" )
--- End code ---
Problem is: If one player uses the command, the cooldown period starts for all players on the server
MrPresident:
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
JasonMan:
--- Quote from: MrPresident 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
--- End quote ---
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?
MrPresident:
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.
Navigation
[0] Message Index
Go to full version