Im trying to inprove on my cooldowns in scripts, so i switched my current system to be server sided so its not exploitable.
The script isnt much and acts like a basic debounce. What im asking is for anything i can use to inprove on the script
client
local event = game.ReplicatedStorage.RemoteEvent
local UIS = game:GetService("UserInputService")
UIS.InputBegan:Connect(function(input, chat)
if chat then return end
if input.KeyCode == Enum.KeyCode.One then
event:FireServer()
end
end)
Server
local event = game.ReplicatedStorage.RemoteEvent
local debris = game:GetService("Debris")
event.OnServerEvent:Connect(function(plr)
local BP = plr.Backpack
local Cooldown = BP.Cooldown
if Cooldown.Value == false then
Cooldown.Value = true
print("cooldown")
task.wait(5)
Cooldown.Value = false
end
end)
local event = game.ReplicatedStorage.RemoteEvent
local debris = game:GetService("Debris")
event.OnServerEvent:Connect(function(plr)
local BP = plr:WaitForChild("Backpack")
local Cooldown = BP:WaitForChild("Cooldown")
corountine.wrap(function() -- Check if I wrote the corountine correctly, I wasn't in roblox studio when I was replying
if Cooldown.Value then return end
Cooldown.Value = true
warn("Cooldown is active!", Cooldown.Value) -- might give an error at Cooldown.Value because printing bool values...
task.wait(5)
warn("Cooldown is inactive!", Cooldown.Value) -- might give an error at Cooldown.Value because printing bool values...
Cooldown.Value = false
end
end)() -- End of corountine function [ do not remove the () else function will not work ]
end)
(I improved readability of the code and some inconsistencies)
Client:
--//Services
local ReplicatedStorage = game:GetService("ReplicatedStorage")
local UserInputService = game:GetService("UserInputService")
--//Variables
local RemoteEvent = ReplicatedStorage.RemoteEvent
--//Functions
UserInputService.InputBegan:Connect(function(input, chat)
if chat then
return
end
if input.KeyCode == Enum.KeyCode.One then
RemoteEvent:FireServer()
end
end)
Server:
--//Services
local ReplicatedStorage = game:GetService("ReplicatedStorage")
local Debris = game:GetService("Debris") --//Don't know why you need this but im adding it anyways
--//Variables
local RemoteEvent = ReplicatedStorage.RemoteEvent
--//Variables
RemoteEvent.OnServerEvent:Connect(function(Player)
local Backpack = Player.Backpack
local Cooldown = Backpack.Cooldown
if not Cooldown.Value then
Cooldown.Value = true
task.delay(5, function()
Cooldown.Value = false
end)
end
end)
(you should always use game:GetService for ReplicatedStorage)
local event = game:GetService("ReplicatedStorage").RemoteEvent
local debris = game:GetService("Debris")
local playerservercd = {}
event.OnServerEvent:Connect(function(plr)
local BP = plr.Backpack
if playerservercd[plr.UserId] ~= nil then
playerservercd[plr.UserId] = true
print("cooldown")
task.delay(5, function()
playerservercd[plr.UserId] = nil
end)
end
end)