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

How Do You Do This Touch Function?

Asked by 8 years ago

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)
0
Do you want it to repeat that next action every time after the first action? M39a9am3R 3210 — 8y
0
I want it to do the first action, then when it is touched again, it does the second action, and then repeats. So it goes back to action 1. GatitosMansion 187 — 8y
0
You could put it in a while loop and wait on the event to occur. In order to get information from events, set variables on the event such as PartThatHit = Part.Touched:wait() Try: while wait() script.Parent.Touched:wait() --Code Here script.Parent.Touched:wait() --Second Code Here end M39a9am3R 3210 — 8y

1 answer

Log in to vote
0
Answered by
Link150 1355 Badge of Merit Moderation Voter
8 years ago

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.

0
Sorry I tried this, but it didn't work for me. GatitosMansion 187 — 8y
0
Oh sorry, I read your question too fast. I'll update my answer. Link150 1355 — 8y
0
Thanks it worked! GatitosMansion 187 — 8y
Ad

Answer this question