Hello, I’m new to raycasting and I’m facing a problem.
I have this raycast function in a weapon tool and it works perfectly.
It’s just that the HumanoidRootPart of the target, blocks the ray from reaching the upper torso or lower torso.
I tried inserting the HumanoidRootPart into the IgnoreList table and it doesn’t seem to work.
function FireGun(Type, MousePos, Target)
if Target then
local gun = script.Parent
if not gun then
return
end
local IgnoreList = {character, gun}
if Target:FindFirstAncestorOfClass("Model"):FindFirstChild("Humanoid") then
table.insert(IgnoreList, Target:FindFirstAncestorOfClass("Model").PrimaryPart)
end
local gunHandle = gun:WaitForChild("Handle", 1000)
local gunTip = gun.Parts.Nodes:WaitForChild("Barrel", 1000)
local barrelPosition = gunTip.Position
local targetPoint
local raycastParams = RaycastParams.new()
raycastParams.FilterType = Enum.RaycastFilterType.Blacklist
raycastParams.IgnoreWater = true
raycastParams.FilterDescendantsInstances = IgnoreList
local rayDirection = gunHandle.CFrame:vectorToWorldSpace(Vector3.new(0, 0, -1))
local ray = Ray.new(gunTip.Position, rayDirection * 3000)
local hit, position = game.Workspace:FindPartOnRayWithIgnoreList(ray, IgnoreList)
targetPoint = position
if Type == "Mouse" then
targetPoint = MousePos
hit = Target
end
local distance = (barrelPosition - targetPoint).Magnitude
if hit and hit.Parent and hit.Parent:FindFirstChildOfClass("Humanoid") then
local targetHumanoid = hit.Parent:FindFirstChild("Humanoid")
dealDamage(hit, targetHumanoid)
elseif hit:FindFirstChild("AccessoryWeld") then
local targetHumanoid = hit:FindFirstAncestorOfClass("Model"):FindFirstChild("Humanoid")
dealDamage(hit, targetHumanoid, hit:FindFirstChild("AccessoryWeld").Part1)
end
end
end
function FireGun(Type, MousePos, Target)
if Target then
local gun = script.Parent
if not gun then
return
end
local IgnoreList = {character, character:FindFirstChild("HumanoidRootPart"), character:WaitForChild("HumanoidRootPart", 3.5), gun}
if Target:FindFirstAncestorOfClass("Model"):FindFirstChild("Humanoid") then
table.insert(IgnoreList, Target:FindFirstAncestorOfClass("Model").PrimaryPart)
end
local gunHandle = gun:WaitForChild("Handle", 1000)
local gunTip = gun.Parts.Nodes:WaitForChild("Barrel", 1000)
local barrelPosition = gunTip.Position
local targetPoint
local raycastParams = RaycastParams.new()
raycastParams.FilterType = Enum.RaycastFilterType.Blacklist
raycastParams.IgnoreWater = true
raycastParams.FilterDescendantsInstances = IgnoreList
local rayDirection = gunHandle.CFrame:vectorToWorldSpace(Vector3.new(0, 0, -1))
local ray = Ray.new(gunTip.Position, rayDirection * 3000)
local hit, position = game.Workspace:FindPartOnRayWithIgnoreList(ray, IgnoreList)
targetPoint = position
if Type == "Mouse" then
targetPoint = MousePos
hit = Target
end
local distance = (barrelPosition - targetPoint).Magnitude
if hit and hit.Parent and hit.Parent:FindFirstChildOfClass("Humanoid") and hit.Name ~= "HumanoidRootPart" then
local targetHumanoid = hit.Parent:FindFirstChild("Humanoid")
dealDamage(hit, targetHumanoid)
elseif hit:FindFirstChild("AccessoryWeld") then
local targetHumanoid = hit:FindFirstAncestorOfClass("Model"):FindFirstChild("Humanoid")
dealDamage(hit, targetHumanoid, hit:FindFirstChild("AccessoryWeld").Part1)
end
end
end
Ignore the code above, you should probably follow what @Dude_Danish is saying because I am not experienced in raycasting at all lol
I think you should keep ignorelist as it is in the code above though.
Ray.new is deprecated. I’d recommend workspace:Raycast instead. In general the code is a bit confusing to me, as you have raycastparams defined and all but it isn’t actually implemented into the ray itself?
Oh right, FindPartOnRayWithIgnoreList is deprecated aswell, I believe you can just delete that as you already got the RaycastParams defined in the third parameter.
workspace:Raycast returns what is called a RaycastResult, which is a table of Data consisting of specific Details / Object of where the Ray was intersected.
You get this Data when it was a Successful Raycast, if a Raycast Fails, or does not reach a destination, the result will return nil, which is why you check if it was sucessful using an if statement, like this:
local rayResult = workspace:Raycast(pos, dir, params) -- the Raycast
if rayResult then -- if the Raycast was successful
local PartOnRay = rayResult.Instance -- Instance Intersected
print(`Intersected {PartOnRay} at {rayResult.Position}`)
else
print(`Nothing was Intersected, Result: {rayResult}`)
end
As far as I know for workspace:FindPartOnRay(), im pretty sure RaycastResult.Instance is its replacement.