Find Touched Part Face

Currently working on a server script that pushes you away from a part when you touch it, it worked just fine by just selecting the LookVector but I wanted it to be more self efficient and just make it so if it touches any part it’ll find which one it was and apply force away from that face, however I have no clue how to find these faces while using Touched, here’s what I got so far.

local parent = script.Parent

debounce = false
--// Checking for what has touched the part
parent.Touched:Connect(function(hit)
	if hit.Parent:FindFirstChild('Humanoid') then
		if not debounce then
			task.wait(0.3)
			debounce = true
			hit.Touched:Connect(function(part)
				--// Checking for what the player is currently touching and if its the parent of this script.
				if part.Name == parent.Name then
					--// Code goes here


					--//^
				end
			end)
			debounce = false
		end
	end
end)
1 Like

Raycast from the parts involved in the collision, for example between the player’s part and the touched part. The result of a successful raycast gives you a Normal, which is the direction you should be pushed away.

1 Like

got this so far but one small issue, the raycast doesnt seem to be able to find the part its supposed to touch.

local parent = script.Parent

debounce = false
--// Checking for what has touched the part
parent.Touched:Connect(function(hit)
	if hit.Parent:FindFirstChild('Humanoid') then
		if not debounce then
			task.wait(0.3)
			debounce = true
			hit.Touched:Connect(function(part)
				--// Checking for what the player is currently touching and if its the parent of this script.
				if part.Name == parent.Name then
					--// Code goes here
					local target = hit.Parent:FindFirstChild('HumanoidRootPart')
					local rayOrigin = hit.Parent.PrimaryPart.CFrame.Position
					local rayDirection = part.CFrame.Position
					local raycastParams = RaycastParams.new()
					raycastParams.FilterDescendantsInstances = {hit.Parent}
					raycastParams.FilterType = Enum.RaycastFilterType.Blacklist
					raycastParams.IgnoreWater = true
					local raycastResult = workspace:Raycast(rayOrigin, rayDirection, raycastParams)
					
					if raycastResult then
						print(raycastResult)
					end
					--//^
				end
			end)
			debounce = false
		end
	end
end)

This is the push force code I use for when you get exploded

|||local pushForce = Instance.new(BodyVelocity,character.HumanoidRootPart)|
|---|---|---|
|||pushForce.MaxForce = Vector3.new(math.huge,math.huge,math.huge)|
|||local directionVector = (character.HumanoidRootPart.Position - otherPlayer.HumanoidRootPart.Position).Unit|
|||local velocity = directionVector * 60
|||pushForce.Velocity = velocity|
|||game.Debris:AddItem(pushForce,0.4)|

Sorry about those weird lines, I don’t want to go through it and delete them lol.

1 Like

RayDirection in your call to Raycast needs to be the direction from the start to the end of your ray, not the position of the end of the ray. So it should be something like part.CFrame.Position - rayOrigin

1 Like

oh oops my bad, I didnt even notice that i kept that there, this works, ill see if i can proceed with this.

Also be careful as the length of rayDirection affects how far along the ray will be checked:
https://create.roblox.com/docs/reference/engine/classes/WorldRoot#Raycast

1 Like

thank you, ill see if i can find it use!, currently making simple stuff to play with LinearVelocity

1 Like

Here’s what I made, thank you @azqjanna and @rubixxman for the help, if you see any issues that could be improved I shall gladly edit them :+1:.

local debugging = true
local parent = script.Parent
local cooldown = 0.4
local jumpHeight = 50

debounce = false
local rayPartConfig = require(script.RayLaserConfig)
--// Checking for what has touched the part
parent.Touched:Connect(function(hit)
	if (hit.Parent:FindFirstChild('Humanoid')~=nil) then
		local char = hit.Parent
		local HRP = char:FindFirstChild('HumanoidRootPart')		
		hit.Touched:Connect(function(part)
			--// Checking for what the player is currently touching and if its the parent of this script.
			if part.Name == parent.Name then
				if not debounce then
					debounce = true
					--// Code goes here
					local target = hit.Parent:FindFirstChild('HumanoidRootPart')
					local rayOrigin = hit.Parent.PrimaryPart.CFrame.Position
					local rayDirection = part.CFrame.Position - rayOrigin
					local raycastParams = RaycastParams.new()
					raycastParams.FilterDescendantsInstances = {hit.Parent}
					raycastParams.FilterType = Enum.RaycastFilterType.Blacklist
					raycastParams.IgnoreWater = true
					local raycastResult = workspace:Raycast(rayOrigin, rayDirection, raycastParams)

					if raycastResult then
						if raycastResult.Normal then
							local raycastNormal = raycastResult.Normal
							local Att0 = Instance.new('Attachment'):Clone()
							local linearVelocity = Instance.new('LinearVelocity'):Clone()

							Att0.Name = 'linearVelocityAtt0'
							Att0.Parent = HRP
							linearVelocity.VelocityConstraintMode = 'Line'
							linearVelocity.RelativeTo = 'World'
							linearVelocity.LineDirection = raycastNormal
							linearVelocity.LineVelocity = jumpHeight*2
							linearVelocity.MaxForce = 100000
							linearVelocity.Attachment0 = Att0
							linearVelocity.Name = 'jumpadLForce'
							linearVelocity.Parent = HRP

							if debugging then
									print('[Jumpad]',hit.Parent.Name,'bounced off',parent.Name,'at',linearVelocity.LineVelocity,'LineVelocity','from',raycastResult.Distance,'studs')
							end

							task.wait(cooldown)
							linearVelocity:Destroy()
							Att0:Destroy()
							--//^
						end
					end
					task.wait(cooldown)
					debounce = false
				end
			end
		end)
		
	end
end)

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.