RemoteEvent cooldown

I have made a remoteEvent that is activated inside starterCharacterScripts. when you press 1.

local key = game:GetService("UserInputService")
local Ability = game.ReplicatedStorage.Ability

key.InputBegan:Connect(function(input)
	if input.KeyCode == Enum.KeyCode.One then
		Ability:FireServer("Really red", 3)
	end
end)

Inside of FireServer I have specified “Really red” and 3 which is my cooldown.

local part = game.Workspace:FindFirstChild("Part")
local Ability = game.ReplicatedStorage.Ability
local Cooldown = false

Ability.OnServerEvent:Connect(function(ability, cooldown)
	if Cooldown == false then
		Cooldown = true
		part.Color = Color3.new(ability)
		task.wait(cooldown)
		Cooldown = false
	end
end)

I have placed the cooldown inside of task.wait but it isn’t registering how would i fix this.

2 Likes

i think you should do something like

key.InputBegan:Connect(function(input)
if input.KeyCode == Enum.KeyCode.One and Cooldown == false then
Cooldown = true
Ability:FireServer(“Really red”,Cooldown)
task.wait(Cooldown)
Cooldown = false
end
end)
2 Likes

I wanted to do it inside server so exploiters can’t access and change the values.

1 Like

what stops them from making the cooldown in the :FireServer to something like 0.001?

1 Like

Because they can’t access the server, at least i assume they can’t.

i’m talking about the input half of it

1 Like

The input doesn’t matter if i can get the cooldown to work, because the input will do nothing until the cooldown is finished serverside.

Nevermind i see what you mean.

it’s because when receiving an OnServerEvent from a Client the Player object automatically gets parsed to the Event like so:

RemoteEvent.OnServerEvent:Connect(function(player) -- insert other arguments here
	print(player) -- prints the player who fired the Remote in the first place
end)

(Server Script) FIX:

this version of your code is safe from any exploiters by using ServerSide Cooldown management.

local part = game.Workspace:FindFirstChild("Part")
local Ability = game.ReplicatedStorage.Ability
local Cooldown = false
local cooldownTime = 3 -- change this to how long you want the cooldown

Ability.OnServerEvent:Connect(function(player, ability)
	if Cooldown == false then
		Cooldown = true
		part.Color = Color3.new(ability)
		task.wait(cooldownTime)
		Cooldown = false
	end
end)

:exclamation:Recommended:

If using Server Side cooldowns I recommend reading this part which shows how to make independent server cooldowns for each player as using the basic “Cooldown” or “Debounce” variable will cause 1 player to be able to fire the code every time the cooldown variable is false rather than each player being able without worrying about when another person last fired it (hence the word independent)

Local Script

local part = game.Workspace:FindFirstChild("Part")
local Ability = game.ReplicatedStorage.Ability
local UserInputService = game:GetService("UserInputService")

local function RandomColor() -- a simple function that returns a random RGB-based color value
	local r, g, b = math.random(1, 255), math.random(1, 255), math.random(1, 255)
	
	return r, g, b
end

UserInputService.InputBegan:Connect(function(input, isTyping) -- basic UIS stuff
	if isTyping then return end
	
	if input.KeyCode == Enum.KeyCode.One then
		local r, g, b = RandomColor()
		
		print(r, g, b)
		
		Ability:FireServer(Color3.fromRGB(r, g, b)) -- convert the color value into RGB format then send it to the server
	end
end)

Server Script

local part = game.Workspace:FindFirstChild("Part")
local Ability = game.ReplicatedStorage.Ability

local cooldownTime = 3 -- change this to how long you want the cooldown
local onCooldown = {} -- a table that holds the players that are on cooldown

Ability.OnServerEvent:Connect(function(player, ability)
	if not table.find(onCooldown, player) then -- if the player isn't on cooldown...
		table.insert(onCooldown, player) -- put them on cooldown here
		
		part.Color = ability -- change the color of the part to whatever the random value generated
	
		task.wait(cooldownTime) -- wait cooldown time
		table.remove(onCooldown, table.find(onCooldown, player)) -- take the player off cooldown
	else
		warn(player, "is on cooldown!") -- if the player tries to fire this code, this will print in the output
	end
end)

This is a more advanced but necessary way of handling Server Sided cooldowns and although it can seem like a bit much effort and confusing for beginners it is vital when making a secure and almost unexploitable game!

I hope this helped! :slight_smile:

1 Like

@MameSace be sure to check over the recommended section i just added for some more advanced cooldown management :raised_hands:t3:

1 Like

Thanks for the help, I’ll try to incorporate this into my script. I did make a table though to handle all my cooldowns.

local part = game.Workspace:FindFirstChild("Part")
local colorChanger = game.ReplicatedStorage.colorChanger
local cooldown = {["Really red"] = 3, ["Royal Blue"] = 6}
local Activated = false

colorChanger.OnServerEvent:Connect(function(plr,Color)
	if Activated == false then
		Activated = true
		part.BrickColor = BrickColor.new(Color)
		task.wait(cooldown[Color])
		part.BrickColor = BrickColor.new("White")
		Activated = false
		plr.Character:ScaleTo(math.random(1, 10))
		plr.Character:WaitForChild("Humanoid").Health = 1

	end
end)

The cooldown table that you have used makes it so that if a player fires that remote and the Part changes to either “Really Red” or “Royal Blue” it will make EVERYONE wait however many seconds you have specified inside of that index {Really Red = 3, Royal Blue = 6}

The one that I used which makes the code run independently for each player (without having to wait if another person has already ran the code) which in some cases may be what you want… It’s typically used in things like Combat systems, Abilities, Attacks, movement etc… but as i state, is the most secure way to handle cooldowns for almost anything you’re making.

I’m unsure on what the code you’re writing is going to be used for but as long as you understand what I mean by this then carry on XD

1 Like

So basically the remote event isn’t individual and if someone fires it everyone else has to wait and your script makes it so the remote event is individual, my script is just practice really I was wondering how to prevent exploiters from tampering with my scripts. The original idea was to have an ability thats unique to each player and when they use it, there is a cooldown that can’t be tampered with.

Yep exactly right, ANY RemoteEvents from a Client to the Server run that code on the Server (everyones computer) by creating a table to keep track of who has fired the code you can then ‘separate’ the people who CAN and CANNOT fire that code depending on if they are found inside of the OnCooldown = {} table!

About Exploits:

to keep this short… anything other than ServerSide scripts (or any core Services that have the word “Server” in them) CAN be tampered with and exploited on the Client (the hackers computer), this includes things like:

  • Local Scripts (including all types of remotes / bindables)
  • GUI
  • Sounds (in some cases)
  • Spawning Parts on their PC
  • Changing their movement speed or jump height
  • Teleportation
  • No-Clipping (settings Part CanCollide to false ONLY on their computer)

Some more advanced exploits can spawn objects which will NOT be displayed to anyone else other than the person using the hacks thanks to FilteringEnabled which is a topic you should look into if you want to learn more about exploiting and writing secure code.

Basically… use the Server to handle anything SUPER important like Data, Cooldowns and any other form of check

This video explains exploits and fighting them well: Stopping Exploits - Suphi Kaner Youtube

1 Like

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