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

How Do I Add An Cooldown On This Script??

Hello, I'm A Beginner In Scripting And I'm Making A Simulator Game And I'm Making A Script And I Don't Know How To Add A Cooldown In Here So Could Anyone Do It?

local Players = game:GetService("Players")
local sword = game.ReplicatedFirst.ClassicSword

local part = script.Parent
local function onTouched(part)  
    local player = Players:GetPlayerFromCharacter(part.Parent)
    if not player then return end
    local backpack = player.Backpack
    sword:Clone().Parent = backpack
end
part.Touched:Connect(onTouched)

2 answers

If you want to add a cooldown to how many times a player can touch, then here is what I would do. Not sure if it will work, but it is good to give it a try!

local Players = game:GetService("Players")
local sword = game.ReplicatedFirst.ClassicSword
local cooldown = false -- Sets the variable cooldown to false


local part = script.Parent
local function onTouched(part) 
 if cooldown == false then-- If cooldown is false then
	cooldown = true -- cooldown will turn into true
    	local player = Players:GetPlayerFromCharacter(part.Parent)
    	if not player then return end
    		local backpack = player.Backpack
    		sword:Clone().Parent = backpack
		wait(10) -- However long you want the cooldown time to be
	cooldown = false [[-- After all the above has runned, cooldown will return to false so it can be touched again!--]]
end

part.Touched:Connect(onTouched)

11 comments

0

It did'nt Work

0

what was the output?

0

The out put Is " Workspace.Part.Sword Giver:8: Expected 'then' when parsing if statement, got 'cooldown' "

0

Lightning just forgot to add "then" to his if statement.

0

yeeaahhh.. i will fix that

0

now it should work

0

now it should work

0

now it should work

0

now it should work

0

now it should work

0

now it should work

To make a cooldown with scripting, we use something called debounce. First, you would assign a value that a player sticks with until they trigger a function and their value is temporarily changed until the function is completed. In order for the function to run, however, the player is required to have a specific value which is their original value. Here's a demonstration:

-- Let's say the script's parent is a TextButton

local debounce = false -- the player starts out with false

script.Parent.MouseButton1Click:Connect(function()
	if debounce == false then -- the player's debounce needs to be false; otherwise, the rest of the code in this if statement will not run
		debounce = true -- if the player clicks the button while their debounce is set to true, the code will not run because the if statement is checking if their debounce is false which it is not anymore
		script.Parent.Text = "Click accepted"

	else
		print("Cooldown is not over yet!")
	end
	
	wait(2) -- player has to wait 2 seconds until their debounce is set back to false again
	debounce = false -- now, they are able to repeat the function
end)

2 comments

0

but debounce is just a variable that is set to false. this means even the word "hello" can be set as the variable instead of debounce, and it will still work.

0

That's right. It doesn't matter what you call it.

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