How can I add a Click Cooldown to this TextButton

Hello! How can I make a delay to clicking the button this script is located too? Such as its on a TextButton. How can I make it so they have a 5 second delay to clicking it, so they can not spam it. How can I do this? This script is my Teleporting script by the way.

here is the code:

-- Functions
local player = game:GetService("Players").LocalPlayer
local playerGui = player:WaitForChild("PlayerGui")
local Player = game.Players.LocalPlayer
local Character = Player.Character
-- Disable walking Functions
local players = game:GetService("Players")
local player = players.LocalPlayer or players.PlayerAdded:wait()
local character = player.Character or player.CharacterAdded:wait()
local humanoid = character:WaitForChild("Humanoid")
-- when you click the button
script.Parent.MouseButton1Click:Connect(function()
	script.Disabled = false
-- See if you own the door
	if Player.Doors[script.Parent.Name].Value == true then
		Character.HumanoidRootPart.CFrame = game.Workspace.DoorsKeys[script.Parent.Name].CFrame
		local debounce = false
			if debounce == false then
			debounce = true
-- disable the walkspeed
			do
				humanoid.WalkSpeed = 0
				humanoid.JumpPower = 0
				
			end
-- Opens the Loading World frame

				game.StarterGui:SetCoreGuiEnabled(Enum.CoreGuiType.All, false)
			playerGui.LoadingWorld.Frame.Visible = true
			
			-- Change Text to Loading...
			playerGui.LoadingWorld.Frame.TextLabel.Text = "Teleporting to Snowy Fields"
			wait(0.5)
			playerGui.LoadingWorld.Frame.TextLabel.Text = "Teleporting to Snowy Fields."
			wait(0.5)
			playerGui.LoadingWorld.Frame.TextLabel.Text = "Teleporting to Snowy Fields.."
			wait(0.5)
			playerGui.LoadingWorld.Frame.TextLabel.Text = "Teleporting to Snowy Fields..."
			wait(0.1)
			-- The Position when it's done
				playerGui.LoadingWorld.Frame:TweenPosition(UDim2.new(1, 0, 0, 0),"InOut","Sine",1, false, function()
					playerGui.LoadingWorld.Frame.Visible = false
					playerGui.LoadingWorld.Frame.Position = UDim2.new(0, 0, 0, 0)
					game.StarterGui:SetCoreGuiEnabled(Enum.CoreGuiType.All, true)
			end)   
			wait(1)
-- reenable walk speed
			do
				
				humanoid.WalkSpeed = 16
				humanoid.JumpPower = 50
				
			end

				task.wait(1)
				debounce = false
			end	
-- If you do not own the location
	else
		script.Parent.TextLabel.Text = "You do not own this Location"
		wait(0.85)
		script.Parent.TextLabel.Text = "Snowy Fields"
	end
-- disables the script
	wait(0.2)
	script.Disabled = false
end)

Any help will be appreciated! :smile:

1 Like

Add a task.wait(5) whenever clicked then activate debounce again.

Where would I put that inside the Script? can you also give an example on what you mean?

At the end of the script. 39 ch ars

Example

`task.wait(5)

debounce = false`

Just add a denounce, put local dB = false at the top, when the button is pressed check if dB is false, if so set it to true and continue your code

Can you send an example on what you mean?

1 Like

On phone but alright.

local db = false

script.Parent.MouseButton1Click:Connect(function()
        if db == false then
            db = true
            Main code here
            wait(cooldown)
            db = false
       end
end)

Okay that worked wow, okay so this is what I did

-- Functions
local player = game:GetService("Players").LocalPlayer
local playerGui = player:WaitForChild("PlayerGui")
local Player = game.Players.LocalPlayer
local Character = Player.Character
-- Disable walking Functions
local players = game:GetService("Players")
local player = players.LocalPlayer or players.PlayerAdded:wait()
local character = player.Character or player.CharacterAdded:wait()
local humanoid = character:WaitForChild("Humanoid")
-- Delay Click Function
local db = false
-- when you click the button
script.Parent.MouseButton1Click:Connect(function()
	if db == false then
		db = true
	script.Disabled = false
-- See if you own the door
	if Player.Doors[script.Parent.Name].Value == true then
		Character.HumanoidRootPart.CFrame = game.Workspace.DoorsKeys[script.Parent.Name].CFrame
		local debounce = false
			if debounce == false then
			debounce = true
-- disable the walkspeed
			do
				humanoid.WalkSpeed = 0
				humanoid.JumpPower = 0
				
			end
-- Opens the Loading World frame

				game.StarterGui:SetCoreGuiEnabled(Enum.CoreGuiType.All, false)
			playerGui.LoadingWorld.Frame.Visible = true
			
			-- Change Text to Loading...
			playerGui.LoadingWorld.Frame.TextLabel.Text = "Teleporting to Snowy Fields"
			wait(0.5)
			playerGui.LoadingWorld.Frame.TextLabel.Text = "Teleporting to Snowy Fields."
			wait(0.5)
			playerGui.LoadingWorld.Frame.TextLabel.Text = "Teleporting to Snowy Fields.."
			wait(0.5)
			playerGui.LoadingWorld.Frame.TextLabel.Text = "Teleporting to Snowy Fields..."
			wait(0.1)
			-- The Position when it's done
				playerGui.LoadingWorld.Frame:TweenPosition(UDim2.new(1, 0, 0, 0),"InOut","Sine",1, false, function()
					playerGui.LoadingWorld.Frame.Visible = false
					playerGui.LoadingWorld.Frame.Position = UDim2.new(0, 0, 0, 0)
					game.StarterGui:SetCoreGuiEnabled(Enum.CoreGuiType.All, true)
			end)   
			wait(1)
-- reenable walk speed
			do
				
				humanoid.WalkSpeed = 16
				humanoid.JumpPower = 50
				
			end

				task.wait(1)
				debounce = false
			end	
-- If you do not own the location
	else
		script.Parent.TextLabel.Text = "You do not own this Location"
		wait(0.85)
		script.Parent.TextLabel.Text = "Snowy Fields"
	end
-- 
	wait(0.2)
	script.Disabled = false
-- 
	wait(1)
	db = false
	end
end)

I saw pretty much this same script on another devforum post, but it seemed to not work, but this worked! I really do appreciate it! :grinning_face_with_smiling_eyes:

1 Like