Scripting Helpers is winding down operations and is now read-only. More info→
Ad
Log in to vote
0

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

Asked by 4 years ago

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)
0
add a wait(1) on line 23 greatneil80 2647 — 4y

1 answer

Log in to vote
0
Answered by
Mayk728 855 Moderation Voter
4 years ago
Edited 4 years ago
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)
0
It works! Thank you for help! OrbitOps 34 — 4y
0
np! :) Mayk728 855 — 4y
Ad

Answer this question