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

How to stop touch detection spamming?

Asked by 6 years ago

I'm working on touch detection, but when I go to test it out, the detection spams between being touched, and the touch ending when I haven't stepped off? What can I do to stop this spam?

local buttonPressed = false

script.Parent.Touched:Connect(function(part)

    local parent = part.Parent



    if game.Players:GetPlayerFromCharacter(parent) and buttonPressed == false then

        local player = game.Players:GetPlayerFromCharacter(parent)
        player.PlayerGui.BuyMoves.Enabled = true
    end




end)




script.Parent.TouchEnded:Connect(function(part)


    local parent = part.Parent

    if game.Players:GetPlayerFromCharacter(parent) then
        buttonPressed = false
        local player = game.Players:GetPlayerFromCharacter(parent)
        player.PlayerGui.BuyMoves.Enabled = false

    end


end)

Thanks!

1 answer

Log in to vote
0
Answered by 6 years ago

Debounce

local debounce = false

something.Touched:connect(function()
    if debounce then return end
    debounce = true
    --code
    wait(.5)
    debounce = false
end)
Ad

Answer this question