Dynamic Cooldowns

I’m looking to create a Cooldown System where I can change the Cooldown once it has already started (I.e: Restart it, finish it sooner than planned, reduce it partially, increase it partially). I’ve been looking around but can’t seem to find anyone that has talked about this topic before? To give an idea, think of DeepWoken’s “Blessed Gem”, where if you manage to land your move, its cooldown is reduced by 50%.
I tried the simple approach of making Cooldowns an IntValue, and setting its value to the Frames it should remain up for, then for reducing/increasing I would just add or remove Frames from that IntValue. Issue is the system works pretty slow, even with only a couple IntValues added, and I have a goal of 25 Players in game, so that’s gonna be tough…

Here is the very rough, clueless Script I made while trying some stuff out:

local PlayerData = script.Parent.PlayerData or game.ReplicatedStorage.PlayerData:FindFirstChild(script.Parent.Parent.UserId)

local Data = {}

for i,v in pairs(PlayerData.Cooldowns:GetChildren()) do
	if v:IsA("IntValue") then
		table.insert(Data, v)
		print(v.Name)
	end
end
for i,v in pairs(PlayerData.States:GetChildren()) do
	if v:IsA("IntValue") then
		table.insert(Data, v)
		print(v.Name)
	end
end

while true do
	for i,v in pairs(Data) do
		if v.Value > 0 then
			v.Value = v.Value - 1
			print(v.Name, v.Value)
		end
	end
	task.wait()
end

As always thanks in advance.

2 Likes

You can utilize workspace:GetServerTimeNow() or os.clock() for this. Depends on what you’re actually trying to do tbh but usually you do cool downs with some sort of timestamp that’s cached on the server for the user and just check if that cool down has been exceeded when they make a network call for an action.

Hmm if this is gonna be on a ServerScript(which it should be if you do not want it to be exploitable) then it should be easy to accomplish via remote events or bindable events.

For example:

local ReplicatedStorage = game:GetService("ReplicatedStorage")

local CooldownInitiate: BindableEvent = ReplicatedStorage:WaitForChild("CooldownInitiate")
local CooldownUpdate: BindableEvent = ReplicatedStorage:WaitForChild("CooldownUpdate")

local PlayerData = script.Parent.PlayerData or game.ReplicatedStorage.PlayerData:FindFirstChild(script.Parent.Parent.UserId)

local Data = {}

CooldownInitiate.Event:Connect(function(PlayerName: string, CooldownProperty, CooldownValue)
    if Data[PlayerName][CooldownProperty].Cooldown.InProggress then warn("This Property is already in cooldown!") return end
	Data[PlayerName][CooldownProperty].Cooldown.Value = CooldownValue
	Data[PlayerName][CooldownProperty].Cooldown.InProggress = true
	Data[PlayerName][CooldownProperty].Cooldown.Thread = coroutine.create(function(PlayerName, CooldownProperty)
		
		while true do
			task.wait(.25)
			if Data[PlayerName][CooldownProperty].Cooldown.InProggress == false then coroutine.yield() end
			
			if Data[PlayerName][CooldownProperty].Cooldown.Value <= 0 then 
				Data[PlayerName][CooldownProperty].Cooldown.Value = 0 
				Data[PlayerName][CooldownProperty].Cooldown.InProggress = false
				return
			end
			
			Data[PlayerName][CooldownProperty].Cooldown.Value -= .25
		end
		
	end)
end)

CooldownUpdate.Event:Connect(function(PlayerName: string, CooldownProperty, UpdateTable)
	for i, v in pairs(UpdateTable)do
		if v == nil then continue end -- Indexes that do not have anything on them will be passed right away.
		
		if i == "Suspend" and v == true then
			Data[PlayerName][CooldownProperty].Cooldown.InProggress = false  -- This is to suspend the loop/coroutine.
			continue
		elseif i == "Suspend" and v == false then
			coroutine.resume(Data[PlayerName][CooldownProperty].Cooldown.Thread) -- This is to resume the suspended coroutine
            Data[PlayerName][CooldownProperty].Cooldown.InProggress = true
			continue
		end
		
		
		if i == "Add" then Data[PlayerName][CooldownProperty].Cooldown.Value += tonumber(v) continue end
		if i == "Subtract" then Data[PlayerName][CooldownProperty].Cooldown.Value -= tonumber(v) continue end
	end
end)

There might be bugs and the such so please do not take this at face value.

This can be done by storing the cooldowns as dictionaries and then modifying them to adjust the cooldowns.

Example:

local AllCooldowns = {
	FirstAttack = {
		ForceStatus = "None";
		LastUsage = os.clock();
		CooldownTime = 7;
	}
}
--[[
To force cooldown on or off, set ForceStatus to either "True" or "False" as STRINGS.
To modify time, subtract (decreases) or add (increases) a number to LastUsage.
]]
function CheckCooldown(Move)
	--//Check if enough time has passed, or if the move is force-readied. Also
	if (Move.CooldownTime + Move.LastUsage < os.clock or ForceStatus == "True") and ForceStatus  ~= "False" then
		return true
	else
		return false
	end
end
2 Likes

I ended up resolving into compromising a bit of the accuracy for performance (Changed the system from using* Frames, to Increments of 0.1 seconds)
So now instead of simply being a task.wait() its a task.wait(0.1). It sacrifices a bit but I figured I wouldn’t need frame-perfect Cooldowns as long as they actually worked, now the margin of error went from up to 2 times the actual Cooldown to about 0.05%!

1 Like

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