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

How do I make a "while touched" function?

Asked by 7 years ago

I want to know how to make a "while touched" script.

So when a player is touching a brick, it will execute a function, but I also want it were the function will always execute intill the player steps off the brick.

So I want a loop when you are touching and i want the loop to end when you stop touching

thank you for any type of help!

1 answer

Log in to vote
1
Answered by 7 years ago
Edited 7 years ago
local block = script.Parent
on = true

function touching()
    print ('Touching!')
end

function notTouching()
    print ('Not Touching!')
end


block.Touched:connect(function(hit)

        if hit.Parent:FindFirstChild("Humanoid") and on == true then --we put this so if item fall in there or the part touching to the ground it will not activated
            touching()
            wait (3)
            on = false
        end
end)

block.TouchEnded:connect(function(hit)
       if hit.Parent:FindFirstChild("Humanoid") and on == false then --same like this one
        notTouching()
        wait (3)
        on = true
    end
end)



i put the comparing variable so it will not spamming printing until 3 second ,sorry if i wrong im still learning too :)

0
Your 'on' variable suffices as a debounce, no need for the extra 3 second wait. What you've written would make it so that you can't leave the part until 3 seconds after touching it, which I would imagine to be inconvenient for players. +1 for the otherwise accurate answer saenae 318 — 7y
Ad

Answer this question