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

How to add a debounce to this script so the player does not spam?

I have a script where players touch a part with tools. I want to add a debounce so the player has to wait before giving it another tool. Can anyone help me with this?

oats = 0
print(oats)
local Allowed = false

function tch(h)
if (h.Parent.Oats.Name == "Oats") then
    oats = oats + 1
    h.Parent:Destroy()
    print(oats)
end
end

script.Parent.Touched:connect(tch)

local Allowed = false

function give(dough)
    if Allowed == true then
        local y = dough.Backpack
        local z = game.Lighting["Handful of Oats"]
        z:Clone().Parent = y
        oats = oats - 1
        Allowed = false
    end
end

function onClicked()
if oats > 0 then
    Allowed = true
end
end

script.Parent.ClickDetector.MouseClick:connect(give)
script.Parent.ClickDetector.MouseClick:connect(onClicked)

1 comment

0

add a wait(1) on line 23

1 answer

✓ Accepted answer
oats = 0
print(oats)

script.Parent.Touched:Connect(function(h)
	if h.Parent.Oats.Name == "Oats" then
		oats = oats + 1
		h.Parent:Destroy()
		print(oats)
	end
end)

local Debounce = false
script.Parent.ClickDetector.MouseClick:Connect(function(dough)
	if oats > 0 and Debounce == false then
		Debounce = true
		local y = dough.Backpack
		local z = game.Lighting["Handful of Oats"]
		--ServerStorage recommended over lighting!
		z:Clone().Parent = y
		oats = oats - 1
		wait(1) --can't do it again for 1 second
		Debounce = false
	end
end)

2 comments

0

It works! Thank you for help!

0

np! :)

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