Answered by
8 years ago Edited 8 years ago
First, let me account for your misconceptions.
- The touched event does not "only" activate "when a part is touched by another part".
Yea, I know, it's weird. The touched event also detects Terrain, Unions, hats, and accessories.
- The touched event works for parts and unions with can collide false, even invisible.
Used quite a lot for invisible detection.
The reason why the Touched event fires even when you're not moving,
The reason this happens is due to the Idle Animation roblox plays when a character is stationary. If you were to move you would also still get more than one touched event firing at once.
How do I detect when a player is on a part using the Touched event?
I actually looked into this and found my own method. I'll be using the TouchEnded
event now as well.
First, let's get our Touched Event Setup,
2 | script.Parent.Touched:connect( function (p) |
Now let's add the touch ended event with an embedded wait() function.
2 | script.Parent.Touched:connect( function (p) |
3 | script.Parent.TouchEnded:wait() |
Cool, now let's add some variables!
3 | script.Parent.Touched:connect( function (p) |
5 | script.Parent.TouchEnded:wait() |
This almost works! All we need is a wait and it will detect when a person is standing/or touching, the part.
3 | script.Parent.Touched:connect( function (p) |
5 | script.Parent.TouchEnded:wait() |
That will actually work. Now anywhere in our script we can check if a player in touching the part by checking if "value" is greater than 0. Here's a loop to demonstrate it working,
03 | script.Parent.Touched:connect( function (p) |
05 | script.Parent.TouchEnded:wait() |
12 | print ( "I'm Being Touched!" ) |
14 | print ( "I'm not being touched" ) |
Now whenever you stand on the part it will print("I'm being touched"), and anytime no one is touching the part it will print("I'm not being touched").
I hope I helped you some.
Good Luck!
If I did help, please don't forget to click the accept answer button. It helps a lot.