Okay. I have written you a ModuleScript in which you can save and retrieve various cooldowns.
I’ve left out meta tables and so on to make it not too complex and I’ve only taken 15 minutes now.
I have written you a server and client script example.
If you want it to be used only by the server, put it in ServerStorage or leave it in ReplicatedStorage and add a runService:IsServer() query in each function.
ModuleScript :
local r_storage = game:GetService("ReplicatedStorage")
local runService = game:GetService("RunService")
local AllPlayers = {}
local cooldownHandler = {}
-- Create the cooldown Table for the Plr
function cooldownHandler.New(Plr : Player)
if AllPlayers[Plr.UserId] then return end
AllPlayers[Plr.UserId] = {
["cooldownmultiplier"] = {TimeToWait = 5, CurrentTask = false},
["testmultiplier"] = {TimeToWait = 10, CurrentTask = false},
}
end
-- Remove the cooldown from the Table
function cooldownHandler.RemovePlayer(Plr : Player)
cooldownHandler[Plr.UserId] = nil
end
-- Start a Timer for the given cooldown
function cooldownHandler.StartCounter(Plr : Player, CooldownName : string)
local FoundValue = AllPlayers[Plr.UserId][CooldownName]
if FoundValue then
local CanStartTask = AllPlayers[Plr.UserId][CooldownName].CurrentTask
if CanStartTask == false then
AllPlayers[Plr.UserId][CooldownName].CurrentTask = true
task.spawn(function()
local DefaultValue = FoundValue.TimeToWait
while AllPlayers[Plr.UserId][CooldownName].TimeToWait > 0 do
AllPlayers[Plr.UserId][CooldownName].TimeToWait -= 1
task.wait(1)
end
AllPlayers[Plr.UserId][CooldownName].TimeToWait = DefaultValue
AllPlayers[Plr.UserId][CooldownName].CurrentTask = false
end)
else
warn("Counter is running")
end
else
warn("Wrong Cooldown Name : ", CooldownName)
end
end
-- Return the Cooldown for a given plr and cooldownName
function cooldownHandler.GetCooldown(Plr : Player, CooldownName : string)
local ValueToReturn = AllPlayers[Plr.UserId][CooldownName]
if ValueToReturn then
return AllPlayers[Plr.UserId][CooldownName]
else
warn("Wrong Cooldown Name : ", CooldownName)
end
end
runService:IsServer()
-- Return the table of a given Player
function cooldownHandler.GetPlayer(Plr : Player)
return AllPlayers[Plr.UserId]
end
-- Return all Players
function cooldownHandler.GetPlayers()
return AllPlayers
end
return cooldownHandler
ServerScript :
local Players = game:GetService("Players")
local r_Storage = game:GetService("ReplicatedStorage")
local CH = require(r_Storage.CooldownHandler)
Players.PlayerAdded:Connect(function(Plr : Player)
CH.New(Plr)
task.wait(5)
warn("Plr Data : ", CH.GetPlayer(Plr))
warn("All Data : ", CH.GetPlayers())
warn("GetCooldown : ", CH.GetCooldown(Plr, "cooldownmultiplier"))
CH.StartCounter(Plr, "cooldownmultiplier")
CH.StartCounter(Plr, "cooldownmultiplier")
task.wait(6)
CH.StartCounter(Plr, "cooldownmultiplier")
end)
Players.PlayerRemoving:Connect(function(Plr : Player)
CH.RemovePlayer(Plr)
end)
r_Storage.RemoteFunction.OnServerInvoke = function(Plr : Player, Key : string, value : any)
if Key == "GetPlayer" then
return CH.GetPlayer(Plr)
elseif Key == "GetCooldown" then
return CH.GetCooldown(Plr, value)
end
end
LocalScript :
local Players = game:GetService("Players")
local r_Storage = game:GetService("ReplicatedStorage")
local Plr = Players.LocalPlayer
task.wait(2)
warn(r_Storage.RemoteFunction:InvokeServer("GetPlayer"))
warn(r_Storage.RemoteFunction:InvokeServer("GetCooldown", "cooldownmultiplier"))
Communication between the client and the server takes place via RemoteFunctions