Detecting if a part is not touched anymore?

Hello!

I’ve been trying to make a gui pop up once the player touches a part, but when the player doesnt touch the part anymore, it get invisible again.

I’ve been trying to do this with TouchEnded, but as we all know, it’s a method that doesn’t really work like it’s supposed to.

Does anybody of you guys know how to do this with GetTouchingParts() or another method?

Thanks!

2 Likes

Have you tried using Magnitude? Magnitude gets the distance between 2 Positions:

if (Part1.Position - HumanoidRootPart.Position).Magnitude < 10 then
    -- do something
end
3 Likes

Would like to add that this doesn’t take into account the size of the two parts, just the middle position in the world.

I think that will help yes, and @lanjtlike is also right, but you can just so Part.Size/2 and you have the size from the middle to the edge.

1 Like

How about ray-casting? It’s a good solution if you have alot of parts like these.
You can make a table of all these parts then cast a ray downwards with whitelist.

If a part gets returned then you’re standing on one of those parts. Only problem with this is, you can only detect parts directly underneath

local plr = game.Players.LocalPlayer
local char = plr.Character or plr.CharacterAdded:Wait()
local mag = script.Parent.Hitter.Size / 2

while wait() do
if (script.Parent.Hitter.Position - char.HumanoidRootPart.Position).magnitude < mag then
	print("yes we're rolling")
else
	print("nah we're not")
end

end

I tried this but it didn’t print anything…

I think magnitude is more efficient? Raycasting is not bad but magnitude is better in my opinion.

it doesn’t work because it doesn’t take into account the distance seperating the humanoidrootpart from the part.

mag = script.Parent.Hitter.Size / 2 + 2.5 --[[You need to account for the size of the 
legs, lowertorso, and half of the humanoidrootpart]]--

thanks but, it still doesnt work…

local plr = game.Players.LocalPlayer
local char = plr.Character or plr.CharacterAdded:Wait()
local mag = script.Parent.Hitter.Size / 2 + 2.5

while wait() do
if (script.Parent.Hitter.Position - char.HumanoidRootPart.Position).magnitude < mag then
	print("yes we're rolling")
else
	print("nah we're not")
end
end

Raycasting at good accuracy would be very resource-heavy, especially raycasting in more than one direction

Workspace.TheRealNcMaster.MagnitudeCalculation:3: invalid argument #2 (Vector3 expected, got number)

1 Like

2 / 2.5 is not a Vector3, but it’s an Int value. These are two different things. Use Vector3.new(x,y,z) instead

Nevermind, got it working. I just had to calculate the Z axis, thanks for the help!

Instead of making mag half the part size(as you cannot compare size with magnitude) just make it something like 5. Because it has to be close enough so that it’s not impossible to active(the HumanoidRootPart is not located on the players feet, it’s at least 2 studs higher than the feet).

So, if your part height was 1, it would never detect the HumanoidRootPart because it’s too high up.


@lanjtlike I can raycast 200 times a second on my Low End Computer without any performance issues.

1 Like

It’s not assuming you raycast effectively. I’m raycasting 3 times per stepped to check for collision and it doesn’t make any dent in performance

That’s not the same for every game and it’s to assume all games run on an effective performance that won’t be effected easily.

No need for raycasting there is a Part.TouchEnded property you can use.

1 Like

If a game is already running bad then they should take care of that, Raycasting, when done correctly and when used in correct situations shouldn’t impact performance.

Sometimes that is out of the developer’s control, and not all people (like new beginners) know how to use raycasting to it’s full performing potential. I’m not arguing about it here but if you’d like to discuss it elsewhere, feel free to do so.

It’s never out of the developer’s control, there are numerous ways to work-around something and virtually anything is possible in game developing. Yes, beginners won’t know how to use raycasting to it’s full potential but that doesn’t mean ray-casting is not a valid option just because they don’t know how to make it work.

Take this very post for example, the OP isn’t sure on how to use the .Touched and .TouchEnded events effectively. It doesn’t mean that using .Touch or .TouchEnded is bad practice.