How to make server side cooldown reset when a player dies

Hello, to protect my game i need server side cooldown but if a player dies while he has an ability on cooldown on the server and respawn he has to wait until the cooldown is finished to activate it.I thoguht about detecting when the player dies and then reset the cooldown but if i wanted to make a lot of abilities this solution can be pretty unoptimized and i also tried to change the key of the cooldown table from player to the character since everytime a player respawns a new character is created thus creating a new cooldown but the problem is the old character is still stored in the table and if the player respawns a lot it will create hundreds of characters inside this table.
TLDR : I need to reset the cooldown on the server when a player dies

in the server sided cooldowns, store the data for the cooldowns in the players character, not the actual player, so that the data gets erased every time he dies, thus giving him no cooldowns after he respawns

I think OP wants the opposite, he wants people to keep their cooldown AFTER they die, so they can’t just reset to get their abilities back. If that’s the case, then you basically do the opposite. You just place it in the player, not the character.

What do you mean by storing cooldown data on the character like putting a server script inside the character

No, i want to reset the cooldown when the player respawns

how do you even do your server side cooldowns, can you give us a sample of your code so we can understand what you are working with?

Oh, my apologies. In that case you would put it in the players character. But I just now realized that you said that your script is gonna be serversided, so I would recommend that you create an IntValue inside of each Player (Not Character) that contains the cooldown. If the Cooldown is == nil or 0, then it will pass, and increase the cooldown, otherwise it will do nothing (Or give the player a notice that there is a cooldown)

There is probably a better way to do this though. I am a beginner/intermediate-ish scripter, so my methods might not always be the best.

Edit: Also, incase you didn’t know, anything stored in the Player will stay for the player’s whole session (When he joins till he leaves, unless you modify something) but anything stored in the Character will reset if the player dies or resets.

Sure here’s how i do them :

local ReplicatedStorage = game:GetService("ReplicatedStorage")
local event = ReplicatedStorage.Events.ExampleEvent

local cooldownTimer = 45
local cooldown = {}

event.OnServerEvent:Connect(function()
replicateProjectile.OnServerEvent:Connect(function(player,timeToTween,mouseCFrame,abilityStartTime)
	local character = player.Character or player.CharacterAdded:Wait()
	
	if cooldown[character] and (os.time() - cooldown[character]) < cooldownTime then return end
	
	cooldown[character] = os.time()
end)

In this example if the character respawns the cooldown would reset but since respawning creates a new character it will add a new character key to the cooldown table, and if the player respawns a lot it will add a lot of keys

I’m in school and the bell rang, so I have to go now. If no one helps you by the time I come back, I will help you.

1 Like

Found a solution instead of creating cooldowns on multiple scripts i’m just going to use a module script.
Here it is :

local CooldownModule = {}

local cooldownPlayer = {}

--How i check a cooldown in a script :
--cooldownPlayer[player] - os.time() < cooldownTime (Let's say 5 seconds)
--os.time() is the time that has elpased since 1970

--Update the time 
function CooldownModule.UpdateCooldown(player: Player)
	cooldownPlayer[player] = os.time()
end

--Remove cooldown
function CooldownModule.ResetCooldown(player: Player)
	cooldownPlayer[player] = 0
end

--Returns the cooldown of a player
function CooldownModule.GetCoodlwon(player: Player)
	return cooldownPlayer[player]
end

return CooldownModule

To reset the cooldown when a player dies :

local ServerStorage  = game:GetService("ServerStorage")
local cooldownModule = ServerStorage.Modules.CooldownModule

game.Players.PlayerAdded:Connect(function(player)
	player.CharacterAdded:Connect(function(character)
		character:WaitForChild("Humanoid").Died:Connect(function()
			cooldownModule.ResetCooldown(player)
		end)
	end)
end)

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