✓ Accepted answer
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,
script.Parent.Touched:connect(function(p)
end)
Now let's add the touch ended event with an embedded wait() function.
script.Parent.Touched:connect(function(p)
script.Parent.TouchEnded:wait()
end)
Cool, now let's add some variables!
local value = 0
script.Parent.Touched:connect(function(p)
value = value+1
script.Parent.TouchEnded:wait()
value = value-1
end)
This almost works! All we need is a wait and it will detect when a person is standing/or touching, the part.
local value = 0
script.Parent.Touched:connect(function(p)
value = value+1
script.Parent.TouchEnded:wait()
wait(.35)
value = value-1
end)
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,
local value = 0
script.Parent.Touched:connect(function(p)
value = value+1
script.Parent.TouchEnded:wait()
wait(.35)
value = value-1
end)
while wait() do
if value > 0 then
print("I'm Being Touched!")
else
print("I'm not being touched")
end
end
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.
5 comments
very helpful, but I actually thought of that concept myself- but my question is if there is an inbuilt function that would detect if a part is continually touched, and it seems that there isnt. but this was helpful none the less, thanks!
No, sadly. There is no inbuilt function. I am going to edit my answer though :3
lol I just noticed that, the moment I finished reading it got an update
is there any way to detect what stopped touching the brick, just like when using something like ''hit'' in a touch event?
Yeah, the touch ended event gives you the part that stopped touching it, script.Parent.TouchEnded:connect(function(part) end)