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

Why does it keep printing "Touched" when I touched it once?

Asked by 2 years ago

Alright, so I have a script that is supposed to print "Touched" once when a player steps on it once, but it keeps duplicatin' the message as fast as hell. Here's my script.

local part = script.Parent

part.Touched:Connect(function(onTouchPart)
    print("Touched")
end)

It's a Script parented to a Part in the Workspace.

I'll be waitin' for answers, y'all.

1 answer

Log in to vote
0
Answered by 2 years ago

You can fix this by adding debounce, here is a quick fix to prevent this.

local part = script.Parent
local debounce = false

part.Touched:Connect(function(onTouchPart)
    if debounce = false then

    debounce = true
    print("Touched")
    wait(3)
    debounce = false

    end
end)

What we are basically doing here is adding a debounce (small wait duration) to the code to prevent it from spamming.

1
NOOOOOOO, theres a reason why CanTouch exists greatneil80 2647 — 2y
1
Yeah, just disable CanTouch, it's practically an in-game debounce. Krektonix_Youtube 85 — 2y
0
Thanks for helping you three! I appreciate your answers that they resolved my problem! isir2204 28 — 2y
Ad

Answer this question