How do you cause the part to do something the first time it's touched, but when you touch it again it does something else, then it does the first action again, and so on in only one script? Thanks!
local debounce = false function onTouched() if not debounce then debounce = true --code function onTouched() -- lol I tried this to see if it works, but it didn't --code end debounce = false end end script.Parent.Touched:connect(onTouched)
Just use a boolean value.
local bool = false local function onTouched(hit) if bool then bool = false -- Do something... else bool = true -- Do something else... end end part.Touched:connect(onTouched)
Additionally, you could shorten your code by replacing bool = false
and bool = true
with a single bool = not bool
before the if ... else ... end
block. That would flip bool
's value whatever its previous value was.