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

about Touched events [?]

Asked by 8 years ago

the Touched event as far as I understand accounts for when a part is touched by another part (with both cancollide properties on) at least, in the way I'm trying to use it.

however, does this event account for if a part is being consistently touched (ex. a player just stands on the part, or object) or is there another event that does? TouchEnded checks to see if a part ''stops touching another part'', so I am wondering about this.

1 answer

Log in to vote
1
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,

-- regular script in part
script.Parent.Touched:connect(function(p)
    --code later
end)

Now let's add the touch ended event with an embedded wait() function.

-- regular script in part
script.Parent.Touched:connect(function(p)
    script.Parent.TouchEnded:wait()
    --code later
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)-- Experiment with this number
    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.
0
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! ProgramsMan 100 — 8y
0
No, sadly. There is no inbuilt function. I am going to edit my answer though :3 User#11440 120 — 8y
1
lol I just noticed that, the moment I finished reading it got an update ProgramsMan 100 — 8y
0
is there any way to detect what stopped touching the brick, just like when using something like ''hit'' in a touch event? ProgramsMan 100 — 8y
0
Yeah, the touch ended event gives you the part that stopped touching it, script.Parent.TouchEnded:connect(function(part) end) User#11440 120 — 8y
Ad

Answer this question