How to make server sided cooldowns for multiple abilities with different cooldowns?

I want each ability to have a different cooldown and debounce, so you cant exploit it trough a local script, but I get the issue that after using an ability once, it cant be used again or sometimes has a longer cooldown, so i want to make it all in one script to handle the cooldowns. Is it possible? If it is, any ideas how I can make it?

Please send the code that is not working so I can better understand the problem.

For each ability you can have two variables, the first is the last time it’s been used and the second is how long it’s cooldown is
The sum of these two would give you the point in time at which the cooldown would be over, so you simply compare it to the current time to check if the cooldown is up or not

local AbilityLastUse = 0
local AbilityCooldown = 10

local function Ability()
   if AbilityLastUse <= time() then
      AbilityLastUse = time() + AbilityCooldown -- reset the cooldown
      -- do your thing
   end
end
2 Likes

Edit 1 : Changed some text
Edit 2 : Add a little bit of code
Edit 3 : Forgot to add an “=” sign

A way to do this is that we need a table called “PlayersTable”. Then, you need to add a table that has the abilities own cooldown and function or action to the “PlayersTable”. , and also insert a nil value to the “PlayersTable”. Like this :

Players.PlayerAdded:Connect(function(Player)
	-- This is just an example
	PlayersTable[Player.UserId] = {
		--[[ [TheAbilityName] = {CooldownTime, function()
			-- do the action
		end} --]]
		["Flip"] = {.5, function()
			print("Flip")
		end},
		["PeopleGoFloat"] = {1.5, function()
			print("Make people go float")
		end}
	}
	
	table.insert(PlayersTable[Player.UserId], nil)
end)

-- If you want to remove the table on the "PlayersTable" when the player leave then.
Players.PlayerRemoving:Connect(function(Player)
	local Index = table.find(PlayersTable, Player.UserId)
	table.remove(PlayersTable[Player.UserId], Index)
end)

Next, you need to make it fire the function :

local function DoAbility(UserId, AbilityName)
	for Name, Table in PlayersTable[UserId] do
		if Name ~= AbilityName then continue end -- You probably known this one already
		
		-- Table[1] is the cooldown, Table[2] is the function
		-- Table[#Table] is the time when the player last used an ability
		if Table[#Table] == nil or os.clock() >= Table[#Table] + Table[1] then
			Table[#Table] = os.clock() -- Set this to os.time() if all of the abilities have no decimal.
			Table[2]() -- This will fire the function.
		end
	end
end

To do the ability :

DoAbility(ThePlayerUserId, TheAbilityName) -- basically fire the function
1 Like

Sorry im late with a reply, but i think this cooldown aplies to all players in the server, how can i fix it?

make it a local script. #characterlimit#

The OP doesn’t want it to be a LocalScript since it can be accessed by exploiter.

When the player joins, add a value to there Player and thats gonna check it

I fixed it by using the comment marked as solution + adding a debounce table for each player

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.