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!
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.
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.
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.