Hello, how would I make a serverwide cooldown for this TextButton? I want the button to only be clicked once per 30 seconds, and print “Error” when the cooldown is active. I’ve tried searching on the DevForum for an understandable solution, but I can’t find anything that I can understand. Any help would be appreciated!
You would need to use a RemoteEvent | Roblox Creator Documentation to make every player have the cooldown.
By the way, I can’t help you without a script provided, so if you have any questions everything will be easier if you send the code you’re working on.
Use a remote function, since it returns data back to the client
It’s not a complicated script.
script.Parent.Activated:Connect(function()
print("Clicked")
end)
How would I use a remote event to create a cooldown? I have little experience with Lua, not really sure what the script would look like.
I would first create a string val in ServerStorage
Don’t forget to define the terms!
– Local Script
local string = game.ServerStorage.StringValue script.Parent.MouseButton1Click:Connect(function() if string.Value == false then print("clicked") string.Value = true remoteEvent:FireServer() end end)
–Server Script
remoteEvent.OnClientEvent:Connect(function(plr) wait(waitTime) string.Value = false end)
You use the remote event to have access to all players, so it would be something like this:
– Local –
local ReplicatedStorage = game:GetService("ReplicatedStorage")
local IsOnCooldown = ReplicatedStorage.IsOnCooldown -- The cooldown bool value
local RemoteEvent = ReplicatedStorage.RemoteEvent
local Tool = script.Parent
local Cooldown = 30 -- Put here the cooldown in seconds
Tool.Activated:Connect(function()
if IsOnCooldown.Value == true then return end
RemoteEvent:FireServer(Cooldown)
end)
– Server –
local ReplicatedStorage = game:GetService("ReplicatedStorage")
local Players = game:GetService("Players")
local IsOnCooldown = ReplicatedStorage.IsOnCooldown
local RemoteEvent = ReplicatedStorage.RemoteEvent
RemoteEvent.OnServerEvent:Connect(function(client, cooldown)
for i, client in pairs(Players:GetPlayers()) do
delay(cooldown, function()
IsOnCooldown.Value = false
end)
IsOnCooldown.Value = true
end
end)
I prefer Remote Functions, even though they’re slow, they can return data from the server.
-- Client Script
button.MouseButton1Click:Connect(function()
local isCooldown = RemoteFunction:InvokeServer()
print(isCooldown)
end)
-- Server script
local clickDebounce = os.clock()
local nextDebounceInterval = 5--seconds
RemoteFunction.OnServerInvoke = function(Player)
if os.clock()-clickDebounce >= nextDebounceInterval then
clickDebounce = os.clock()
return "Yes!"
else
return "COOLDOWN!"
end
end
That will not work, since client can’t access ServerStorage.
Thank you! How would I also make it print “error” if the cooldown is active?
I think it’s not necessary to use a for loop here?
Take mine for an example, it uses RemoteFunctions, which returns something
On the LocalScript there’s an if statement, which detects if the Value is true, so you can just do:
if IsOnCooldown.Value == true then
error("Cooldown active")
return
end
Doesn’t work, ‘RemoteFunction’ isn’t defined.
It doesn’t work, but I’m pretty sure it’s because I did something wrong. Here’s the LocalScript. The “Clicked” print works, but the “Cooldown active” doesn’t.
local ReplicatedStorage = game:GetService("ReplicatedStorage")
local IsOnCooldown = ReplicatedStorage.IsOnCooldown -- The cooldown bool value
local RemoteEvent = ReplicatedStorage.RemoteEvent
local Tool = script.Parent
local Cooldown = 5 -- Put here the cooldown in seconds
Tool.Activated:Connect(function()
if IsOnCooldown.Value == true then return end
print("Clicked")
RemoteEvent:FireServer(Cooldown)
if IsOnCooldown.Value == true then
print("Cooldown active")
return
end
end)
Create a remote function then, its pretty self explanatory
all you need to do is to define the remote function like how Sarchyx define RemoteEvent
Yea… you need to replace it, not to create a new if statement:
local ReplicatedStorage = game:GetService("ReplicatedStorage")
local IsOnCooldown = ReplicatedStorage.IsOnCooldown -- The cooldown bool value
local RemoteEvent = ReplicatedStorage.RemoteEvent
local Tool = script.Parent
local Cooldown = 5 -- Put here the cooldown in seconds
Tool.Activated:Connect(function()
if IsOnCooldown.Value == true then
error("Cooldown active")
return
end
RemoteEvent:FireServer(Cooldown)
end)
My bad, it works now. Thank you!
This didn’t work either, but no sweat. The script provided by RVCDev works.