Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
✓ ANSWER ACCEPTED

Adding a Timer Between Everytime A Player Clicks A Block?

I know the title isnt worded the best but I shall elaborate here.

I have a teleport script, and the way you activate it is by clicking a GUI and I was wanting a cooldown between every click, lets say 5 seconds for example. I added a wait to the script but it doesnt do what id like. I am thinking I may have to turn it into in "if and" statement or a "repeat" statement but I dont know for sure.

Heres the script :) Thanks for your time

local plr = game.Players.LocalPlayer --This is the client
local char = plr.Character or plr.CharacterAdded:Wait() --This is the character
local root = char:WaitForChild("HumanoidRootPart") --This is the HumanoidRootPart
local teleportTo = workspace.FrontTeleport --This is where you teleport to!
local guiButton = script.Parent --This is the button you're clicking!

 guiButton.MouseButton1Down:Connect(function() --MouseButton1Down event
	root.CFrame = teleportTo.CFrame * CFrame.new(0,3,0) --Set the root's CFrame to the teleportobject's CFrame, translate it up by 3 studs
end)

1 answer

✓ Accepted answer

An easy way to add a cooldown is to use a Debounce variable.

Debounces prevent code in events from executing unnecessarily.

local plr = game.Players.LocalPlayer
local char = plr.Character or plr.CharacterAdded:Wait()
local root = char:WaitForChild("HumanoidRootPart")
local teleportTo = workspace.FrontTeleport
local guiButton = script.Parent
local db = false --This is your debounce!
local cooldown = 1 --This is the cooldown time

 guiButton.MouseButton1Down:Connect(function()
	if not db then --Check the debounce prior to executing code
		db = true --Turn it on so while its on there are no duplicate firings
		root.CFrame = teleportTo.CFrame * CFrame.new(0,3,0)
		wait(cooldown) --wait the duration of the cooldown
		db = false --Turn the debounce off so the function can execute again
	end
end)

This discussion is preserved as it was. New answers, comments, and votes are closed.