Need help with loop code and GetTouchingParts

Hello, I am trying to make it so that if a player is touching a part then it will set a value to true, if they are not then it sets it to false. Right now my code is setting it to true and then to false over and over.
If you can help, that’d be awesome!
Here is the code:

	while true do
		local children = Part:GetTouchingParts()
		
		for i, child in ipairs(children) do
			if child.Parent.Name == player.Name then
				print("Player Is Touching")
				isPlayer = true
			else
				print("Player Is NOT TOUCHING")
				isPlayer = false
			end
		end
		
		if breaker == true then
			break
		end
		wait()
	end

I believe :GetTouchingParts() does work with the .Touched function so this would obviously happen, whenever they are inside of it and not moving (like an animation or actual movement) the object is not being touched however when they are waking on it, it activates on and off alot. I still don’t see why Roblox hasn’t fixed this

1 Like

You could use .TouchEnded which triggers when the thing stops touching the part.

BasePart.TouchEnded (roblox.com)

I’d love to, but roblox hasn’t fixed that function either.

And .Touched to detect if they are touching the part.

BasePart.Touched (roblox.com)

Both functions are broken. They haven’t fixed to where it doesn’t activate if they are already on it / actually touching it.

1 Like

Oh, I see. I thought it worked cause it still works for me.

.Touch does work but thats just detecting whether a player has touched a part, not if they’re not

I do use .Touched for some objects but I usually add a debounce to help with the issue stated that these functions don’t really work.

Any more advanced options that work, I’m all ears

I have, I just want to detect if the player is not longer touching the part

This would work very well cause you could detect if a player was touching it from the touching parts.

Isn’t that what he’s currently using.

Yep, but it doesn’t work too well because it is picking up other object touching it too.

Cause I’ve said "If touching part.Parent.Name ~= player.Name then

I was saying that :GetTouchingParts() is basically just .Touched but with a table of parts accessible to ReadOnly

1 Like

You may be able to use a ray cast below the player to see if the object is below it. Or magnitude this is the only other option I know.

Get the bounding box of the player (they’re a model in the workspace), get the position of the part you want to check isn’t being touched by the player, if the bounding box of the player’s character model and the part in question intersect then they are touching, otherwise they are not touching.

https://developer.roblox.com/en-us/api-reference/function/Model/GetBoundingBox

What you could do is place the part in a single model itself, then perform the GetBoundingBox function on both the model (which only contains the single part) and the character model.

1 Like

Yoo, this looks promising. I’ll get back to you if it doesn’t work though lol. Thanks for replying

Both of the functions do work, they just don’t behave in the way that you expect.

Touched is fired whenever a part touches another part.
TouchEnded is fired whenever a part is no longer touching another part after they have already touched.

There is no event which detects whether or not a part is not touching another part (if they haven’t already touched before).