How do i use delay in tools?

I have a problem with my tool, If i click alot the tool’s script fires too many times.

What do i do to make a delay in my tool?

1 Like

I believe you are looking for a debounce. This prevents a function from running too many times. I suggest you implement a debounce on the client and server so exploiters won’t just simply disable it locally to get around it and attack the server.

Example Debounce:

local Debounce = true
local Delay = 5

local function Action()
    if Debounce then
        Debounce = false
        --code
        wait(Delay)
        Debounce = true
    end
end
3 Likes

im using instance.new in the script

it plays the sound but doesnt make a new explosion

full code:

local model = script.Parent
local Debounce = true
local Delay = 5

script.Parent.Activated:Connect()
if Debounce then
Debounce = false
model.Handle.BombBeeping:Play()
redlight = Instance.new(“SpotLight”)
redlight.Color = Color3.fromRGB(255,0,0)
redlight.Parent = model.Handle
redlight.Brightness = 100
redlight.Range = 100
wait(1)
model.Handle.BombBeeping:Stop()
model.Handle.Boom:Play()
model.MainVest.CanCollide = false
ex = Instance.new(“Explosion”)
ex.Position = script.Parent.MainVest.Position
ex.Parent = workspace
ex.BlastRadius = 10
redlight:Destroy()

    wait(5)
    Debounce = true
end)

wait nvm it just errors and it doesnt fire anything.

Is there something other than debounce?

What errors are you getting? Also, your code didn’t format very well on your post, so it’s fairly messy and hard to read.

You may want to try this:

local model = script.Parent
local Debounce = true
local Delay = 5

script.Parent.Activated:Connect(function()
	if Debounce then
		Debounce = false
		model.Handle.BombBeeping:Play()
		redlight = Instance.new("SpotLight")
		redlight.Color = Color3.fromRGB(255,0,0)
		redlight.Parent = model.Handle
		redlight.Brightness = 100
		redlight.Range = 100
		wait(1)
		model.Handle.BombBeeping:Stop()
		model.Handle.Boom:Play()
		model.MainVest.CanCollide = false
		ex = Instance.new("Explosion")
		ex.Position = script.Parent.MainVest.Position
		ex.Parent = workspace
		ex.BlastRadius = 10
		redlight:Destroy()
	    wait(5)
	    Debounce = true
	end
end)

Also, read this post on how to post code on the forum

Thank you this script worked. (i will learn from you, also ill read the post on how to post code on the forum)

The error log thing is a mess so i cannot know what error i got.