How would I make a cooldown for this?

I made a script that teleports you forward, inverts colors and plays a sound. It works perfectly, but I need to make a cooldown for it and I have no idea how.

local lighting = game.Lighting
local plr = game.Players.LocalPlayer
local char = plr.Character or plr.CharacterAdded:Wait()
local root = char:WaitForChild("HumanoidRootPart")

game:GetService("UserInputService").InputBegan:Connect(function(Input,gameProcessed)
    if not gameProcessed then
        if Input.KeyCode == Enum.KeyCode.Q then
			            root.CFrame = root.CFrame * CFrame.new(0,0,-35)
			lighting.ColorCorrection.Saturation = -1
			lighting.bruh.Saturation = -1
			workspace.Sound:Play()
			wait(0.4)
			lighting.ColorCorrection.Saturation = 0
			lighting.bruh.Saturation = 0
			
        end
    end
end)

Here is the script

3 Likes

local cooldown = false
local cooldownTime = 10
local lighting = game.Lighting

local plr = game.Players.LocalPlayer
local char = plr.Character or plr.CharacterAdded:Wait()
local root = char:WaitForChild("HumanoidRootPart")

game:GetService("UserInputService").InputBegan:Connect(function(Input,gameProcessed)
if not cooldown then
cooldown = true
    if not gameProcessed then
        if Input.KeyCode == Enum.KeyCode.Q then
			            root.CFrame = root.CFrame * CFrame.new(0,0,-35)
			lighting.ColorCorrection.Saturation = -1
			lighting.bruh.Saturation = -1
			workspace.Sound:Play()
			wait(0.4)
			lighting.ColorCorrection.Saturation = 0
			lighting.bruh.Saturation = 0
			
        end
    end
else
wait(cooldownTime)
cooldown = false
end
end)

Hope it helps!

1 Like

Thank you!

(30 character limit)

Your welcome! Have a great day :smiley:

Hold on, Apparently I can spam it to get like 6 off, then the cooldown will work. Any fix?

Well, i dunno ill think ill come back if I have an solution.

Thank you, i’ll be waiting

(30 character)

Hmm weird I wonder… (30 char)

Bump because I need help with this

Not sure why with your script I can use it multiple times then it just stops. Maybe it’s got to do with the line being at the bottom? I’m not sure, I’m pretty new to scripting so I’m seeking some help here

Im afraid @happle5222 didn’t explain what he done very much, he used a basic Debounce.

Debounces are possibly one of the most simple logic style functions in Lua, they can be made to fit pretty much any application. Let me give a quick example…

local Debounce = false
local DebounceCoolDown = 1

if Debounce == false then
Debounce = true

-- Your code here

wait(DebounceCoolDown)
Debounce = false
end 

So basically the way this works is you have a global variable(95% of the time), that variable is either set to true or false, doesn’t matter which you start with… When the event happens, whether its a Touched, Activated, etc, it will go through this code to start with…

Since we declared the Debounce to be equal to false, the first thing it is going to do is see if that value is true or false, if its false like we said it is, its going to continue with the script, but notice the next line, we’re going to set that value to true… Meaning that if you try to run it again without setting it back to false, its not going to run the code since its stopping on the if statement. After that all of you code goes there, At the end, we’re going to be setting Debounce back to false so it can run again, but since we want a cool down, we’re going to delay that a bit with a basic wait()…

Any questions?

1 Like

And to just give another quick example since I didn’t include much on how to implement it into your code, here is a basic touched event…

local Debounce = false
local DebounceCoolDown = 1

script.Parent.Touched:Connect(function()
	if Debounce == false then
		Debounce = true
		print("Watch where you're walking")
		wait(DebounceCoolDown)
		Debounce = false
	end
end)

Since I don’t want it spamming output with “Watch where you’re walking” 100+ times a second, I set it to have a simple debounce. This means that it’ll print a max of 1 time per second… Feel free to take this code and play around with it a ton to figure out exactly how it works.

local Debounce = true
local DebounceCoolDown = 1

script.Parent.Touched:Connect(function()
	if Debounce == true then
		Debounce = false
		print("Watch where you're walking")
		wait(DebounceCoolDown)
		Debounce = true
	end
end)

Heres another example just because Im bored, bascially the exact same thing as I said above. Notice how all of the values are switched though, now the original value is set to true… Logically it will work the same though… It’ll check to see if Debounce is true, if it is, it’ll set it to false and wait for it to go back to true

1 Like

Thank you! I’ll definitely try it out.

1 Like

Alright, I’m not sure how to do it. This is creating an error, what did I do wrong?

local Debounce = false
local DebounceCoolDown = 1
local lighting = game.Lighting
local plr = game.Players.LocalPlayer
local char = plr.Character or plr.CharacterAdded:Wait()
local root = char:WaitForChild("HumanoidRootPart")

game:GetService("UserInputService").InputBegan:Connect(function(Input,gameProcessed)
    if not gameProcessed then
		        if Input.KeyCode == Enum.KeyCode.Q then
			if Debounce == false then
Debounce = true

			            root.CFrame = root.CFrame * CFrame.new(0,0,-35)
			lighting.ColorCorrection.Saturation = -1
			lighting.bruh.Saturation = -1
			workspace.Sound:Play()
			wait(0.4)
			lighting.ColorCorrection.Saturation = 0
			lighting.bruh.Saturation = 0
			
        end
    end
end

Woops, forgot to add the wait(debouncecooldown), However it still creates the same error.
image

Alright, so that error is because the final end is missing a ), should be “end)” since its closing the function. Lastly, you forgot to last couple lines to the debounce that sets the value back to false, refer to the examples above, notice how at the end of each I had 2 lines, the wait and returning the Debounce value to the original value

Just paid attention and saw you already noticed the last 2 lines, sorry

1 Like

It worked! Thank you for your help.

1 Like