How do I make a ray cast between 2 HumanoidRootParts?

Hello dev’s, so today I was working with raycasts when then I wanted to make a raycast between 2 HumanoidRootParts. I also wanted to print a parts name if It was between the 2 HRPs. I don’t know 100% if I got it working.
Here is my code:

game:GetService("Players").PlayerAdded:Connect(function(player)
	player.CharacterAdded:Connect(function(character)
		local humanoidrootpart = character:WaitForChild("HumanoidRootPart")
		local start = humanoidrootpart
		local finish = workspace:FindFirstChild("Dummy").HumanoidRootPart
		local raystart = start.Position
		local raydestination = finish.Position
		local raydirection = raydestination - raystart
		local ray = Ray.new(raystart,raydirection)
		local part = workspace:FindPartOnRay(ray)
		print(part.Name)
	end)
end)

If something doesn’t look right please inform me. Thank you for your help!

1 Like

Everything here looks fine except for 2 things.

the first is that workspace:FindPartOnRay() is Deprecated and has be replaced with workspace:Raycast(). workspace:Raycast() uses RaycastParams object that store some info about the Raycast.

and heres a link to the Raycast funciton api reference: WorldRoot | Roblox Creator Documentation
and heres one for the RaycastParam: RaycastParams | Roblox Creator Documentation

The second is that they ray will probably intersect the HumanoidRootParts that you are trying to cast a ray in between. To fix this you should make it so that they are ignored when raycasting.
This can be done by setting the RaycastParams.FilterDescendantsInstances to a table with all the things you want to ignore, aka the two HumanoidRootParts, and then setting RaycastParams.FilterType to Enum.RaycastFilterType.Blacklist.

1 Like

Could you maybe show an example on what you just explained? That would help a lot.

game:GetService("Players").PlayerAdded:Connect(function(player)
	player.CharacterAdded:Connect(function(character)
		local humanoidrootpart = character:WaitForChild("HumanoidRootPart")
		local start = humanoidrootpart
		
		--storing dummy in a variable for later use
		local dummy = workspace:FindFirstChild("Dummy")
		
		local finish = dummy.HumanoidRootPart
		
		local raystart = start.Position
		local raydestination = finish.Position
		local raydirection = raydestination - raystart
		
		
		--creating a new raycastParam to store some info about the raycast
		local raycastParams = RaycastParams.new()
		
		--putting dummy and character in the ignore list so that they both are ignored when raycasting
		raycastParams.FilterDescendantsInstances = {character, dummy}
		
		--setting the filter type to black list
		--black list makes it so that the ray only hits things that are not on the list
		raycastParams.FilterType = Enum.RaycastFilterType.Blacklist
		
		---workspace:Raycast returns a RaycastResult object if it hits something and nil if it hits nothing
		local result = workspace:Raycast()
		

		---checking to see if we hit anything
		if result then
			local part = result.Instance
			print(part.Name)
		else
			print("hit nothing")
		end
		
	end)
end)

heres the changes that I would make with comments for bit of explanation.

2 Likes

This line of code:

Brings an error “Argument 2 missing or nil”. I don’t know if thats what it says if there is no parts between the HRPs.

whoops my bad
it should be local result = workspace:Raycast(raystart, raydirection * (raystart - raydestination).magnitude)

4 Likes