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

How do I add a cool down to this script?

I want to add a debounce to this script and I am not sure how. Any help?

function onActivated()
	if not enabled  then
		return
	end

	enabled = false
	Tool.GripForward = Vector3.new(0,-.759,-.651)
	Tool.GripPos = Vector3.new(1.5,-.5,.3)
	Tool.GripRight = Vector3.new(1,0,0)
	Tool.GripUp = Vector3.new(0,.651,-.759)


	Tool.Handle.DrinkSound:Play()

	wait(3)
	
	local h = Tool.Parent:FindFirstChild("Humanoid")
	if (h ~= nil) then
		if (h.MaxHealth > h.Health + 5) then
			h.Health = h.Health + 5
		else	
			h.Health = h.MaxHealth
		end
	end

	Tool.GripForward = Vector3.new(-.976,0,-0.217)
	Tool.GripPos = Vector3.new(0.03,0,0)
	Tool.GripRight = Vector3.new(.217,0,-.976)
	Tool.GripUp = Vector3.new(0,1,0)

	enabled = true

end

function onEquipped()
	Tool.Handle.OpenSound:play()
end

script.Parent.Activated:connect(onActivated)
script.Parent.Equipped:connect(onEquipped)

1 comment

0

there is no else statement aswell, in the solution I added one with extra wait() time.

1 answer

✓ Accepted answer

I know you know that basically a debounce is simply a variable that alternates between values, I think you should try not using return like so. (PS: https://blackmiaool.com/lua-beautify/ can clean up your scripts fast, but can also break them) Ok try this, I can't say it will for sure work


local enabled = true

function onActivated()
    if enabled == true then
	enabled = false
    Tool.GripForward = Vector3.new(0,-.759,-.651)
    Tool.GripPos = Vector3.new(1.5,-.5,.3)
    Tool.GripRight = Vector3.new(1,0,0)
    Tool.GripUp = Vector3.new(0,.651,-.759)


    Tool.Handle.DrinkSound:Play()

    wait(3)

    local h = Tool.Parent:FindFirstChild("Humanoid")
    if (h ~= nil) then
        if (h.MaxHealth > h.Health + 5) then
            h.Health = h.Health + 5
        else    
            h.Health = h.MaxHealth
        end
    end

    Tool.GripForward = Vector3.new(-.976,0,-0.217)
    Tool.GripPos = Vector3.new(0.03,0,0)
    Tool.GripRight = Vector3.new(.217,0,-.976)
    Tool.GripUp = Vector3.new(0,1,0)

    wait(1)
    enabled = true

else
	wait(4)
	enabled = true
end

function onEquipped()
    Tool.Handle.OpenSound:play()
end

script.Parent.Activated:connect(onActivated)
script.Parent.Equipped:connect(onEquipped)

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