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

Can somebody show me how to give this a cooldown?

This is force pull, people keep spamming the key. Is there a way to cooldown it?

	wait(5) -- Wait for the Character to load

 

 

 

local User = game.Players.LocalPlayer

 

local mouse = User:GetMouse()

 

Animation = Instance.new("Animation")

 

Animation.AnimationId = "http://www.roblox.com/Asset?ID=166143393"

 

Animation.Name = "Force pull."

 

local animTrack = User.Character.Humanoid:LoadAnimation(Animation)

 

forceValue = -200 -- The ammount of force to use, for easy changeing

 

 

 

function ForcePush(key) --This function is run every time that a person presses a button on the Keyboard

    if key == "e" then -- Checks if the button is F

        if mouse.Target ~= nil and mouse.Target:IsA("BasePart") then -- Checks if the mouse is over a part

            if mouse.Target.Parent:IsA("Hat") then -- If its over a hat

                animTrack:Play() --Plays Anim

                mouse.Target.Parent.Parent.Torso.Velocity = (workspace.CurrentCamera.CoordinateFrame.lookVector*forceValue) + Vector3.new(0,70,0) -- Forces
            elseif mouse.Target.Parent:FindFirstChild("Humanoid") then -- If its over a body part

                animTrack:Play() --Plays Anim

                mouse.Target.Parent.Torso.Velocity = (workspace.CurrentCamera.CoordinateFrame.lookVector*forceValue) + Vector3.new(0,70,0) -- Forces

            end

        end

    end

end

 

 

mouse.KeyDown:connect(ForcePush)


1 answer

You use a debounce.

A debounce is a variable, usually a boolean, that starts with a certain value. Then you use an if statement to check if it has that value. If it does, then you change the value. Now look what you did. You prevented the script from getting past the if statement!

local debounce = false

if debounce == false then
	debounce = true --Now your code will not run, because debounce is not false.
	--stuff
end

Of course, you need to set it to false again, or your code will never again execute. If you want a certain amount of time to pass before the code can run again, just use wait()

local debounce = false

if debounce == false then
	debounce = true --Now your code will not run, because debounce is not false.
	--stuff
	wait(5)
	debounce = false --Now your code will run again.
end

Hope i helped!

2 comments

0

You put wait(5) debounce = true.

0

Thank you.

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