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

How can I prevent this?

Asked by 10 years ago

I am writing a script, that triggers when the player steps on something. I set this up to test if it would work.

local touching = false
script.Parent.Trigger.Touched:connect(function(hit)
    if not touching then
        print("Touched")
        touching = true
    end
end)
script.Parent.Trigger.TouchEnded:connect(function(hit)
    if touching then
        print("TouchEnded")
        touching = false
    end
end)

As a player walks on it, it triggers it multiple times EG, my output window TouchEnded Touched TouchEnded Touched TouchEnded Touched TouchEnded

How can I prevent this from happening?

0
On touchended you set the variable back to false, so it can run again:o Thetacah 712 — 10y

1 answer

Log in to vote
0
Answered by 10 years ago

Use a Debounce. A Debounce prevents the event to happen infinite times.

touching = false --Local not necessary
debounce = true
script.Parent.Trigger.Touched:connect(function(hit)
    if not touching and debounce == true then
        print("Touched")
        touching = true
    debounce = false
    wait(1)
    end
end)
script.Parent.Trigger.TouchEnded:connect(function(hit)
    if touching and debounce == true then
        print("TouchEnded")
        touching = false
    debounce = false
    wait(1)
    debounce = true
    end
end)
0
You didn't set debounce to true in the first one. Fix this. xD And local IS necessary. It loads variables quicker, and it's nicer formatting. Tkdriverx 514 — 10y
Ad

Answer this question