Feedback on first server sided cooldown script?

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)

Thank you for your time :grinning:

1 Like

try this if it will improve your script

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)
1 Like

for more of Debounce / Cooldown, Check this >

Debounce / Cooldown

1 Like

Please use descriptive names!

-- UserInputService not UIS
local UserInputService = game:GetService("UserInputService")
-- player not plr
event.OnServerEvent:Connect(function(player)
    -- Backpack not BP
    local Backpack = player.Backpack
    -- ...
1 Like

This does not mess with the code in any way nor is it bad practice and is completely up to preference…

3 Likes

I get that it’s preference, but it really does help with being able to read your code later.
See Roblox Lua style guide.

(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)

1 Like
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)
1 Like