Detect a touch only from one side of a part

I want to detect a touch only from one side.
Currently my code is just a normal .Touched event and the door opens only by a key, but I want to have a one-sided door. Basically, you can open the door with a key only from one side, and not from the other.
Any thoughts on how to do that?

You can use math with dot product or local space conversion to detect what side the player is on; or you can place another invisible part and use a touched event on that

3 Likes

As @Thedagz said, you can check where the player is when they touch the part.
Here’s an simple example:

script.Parent.Touched:Connect(function(hit)
	if hit.Parent.HumanoidRootPart.Position.Z - script.Parent.Position.Z > 0 then
		print("front")
	else
		print("back")
	end 
end)
1 Like

Good method although it doesn’t take account of the door rotation so using cframe would be preferable if using this type of method :slight_smile:

Door.CFrame:Inverse() * Character.Position

Although I believe using the door lookvector and using dot product is more efficient

4 Likes

I’d recommend for simplicity just having an invisible part on one side of the door which causes the door to be opened (make sure you can’t clip from the other side of the door to trigger the “Touched” event though).

1 Like