How would i make a cooldown on a function? (like with CurTime)

So I have a function:


function org.InviteMember(org,ply,perms, args)
	if org != nil then
		if OG.CheckPermission(perms, "-i") then
			-- invite member
			local plyS = args[1]
			if plyS == ply then OG.SendMessage(ply, "You cannot invite yourself!") return end
			if plyS:GetOrg() == nil then
				-- Send invite
				org.invites[plyS:UniqueID()] = "Received"
				OG.RefreshOrgData(plyS:GetOrg())
				DarkRP.createQuestion("Would you like to join " .. ply:GetOrg():GetName() .. "! 
 -Invited by " .. ply:Nick(), "memberinvite".. ply:UniqueID(), plyS, 30, QFunc, ply, plyS )
			else
				OG.SendMessage(ply, "That player is already in an organisation!")
				-- Different org
			end
		else
			OG.SendMessage(ply, "You do not have the required permissions to invite a member!")
			-- doesnt have required perms
		end
	else
		OG.SendMessage(ply, "You are not in an organisation!")
		-- Not in an org
	end
end

How would I go about making a cooldown for this because I found that you can spam this on someones screen and it’s really annoying. I was experimenting with CurTime() and I have no idea on how to use it. Please help.

I assume this is serverside.

A table indexed by player names containing the time when a player is allowed to resend an invite should work.



local playerInviteTimers = {}
local inviteDelay = 15

-- setup the table
for _, ply in pairs( player.GetAll() ) do
  
  playerInviteTimers[ ply:SteamID() ] = 0
  
end

local invitePlayer = function( ply, args )
  
  if playerInviteTimers[ ply:SteamID() ] < CurTime() then
    
    sendInvite()
    playerInviteTimers[ ply:SteamID() ] = CurTime() + inviteDelay
    
  else
    
    dontSendInvite()
    
  end
  
end



This should be simple enough to add to your code, though maybe in a different format.

[lua]
function org.InviteMember(org,ply,perms, args)

if ply.LastInvite then --Check if variable exists.
	if ply.LastInvite &gt; CurTime() then --Check if they still have a time delay.
		OG.SendMessage(ply, "You are sending invites too fast!") --Inform client.
		return --End the function entirely.
	end
end

ply.LastInvite = CurTime() + 5 --Set new delay to 5 seconds in the future.


if org != nil then
	if OG.CheckPermission(perms, "-i") then
		-- invite member
		local plyS = args[1]
		if plyS == ply then OG.SendMessage(ply, "You cannot invite yourself!") return end
		if plyS:GetOrg() == nil then
			-- Send invite
			org.invites[plyS:UniqueID()] = "Received"
			OG.RefreshOrgData(plyS:GetOrg())
			DarkRP.createQuestion("Would you like to join " .. ply:GetOrg():GetName() .. "! 

-Invited by " … ply:Nick(), “memberinvite”… ply:UniqueID(), plyS, 30, QFunc, ply, plyS )
else
OG.SendMessage(ply, “That player is already in an organisation!”)
– Different org
end
else
OG.SendMessage(ply, “You do not have the required permissions to invite a member!”)
– doesnt have required perms
end
else
OG.SendMessage(ply, “You are not in an organisation!”)
– Not in an org
end
end
[/lua]

[editline]22nd December 2016[/editline]

You should change ply.LastInvite to ply.NextAllowedInvite for it to make more sense.

Crap man, thank you so much. It works perfectly!

Try to learn from it. Very simple but very effective. The only things I added were at the top, commented.