I recommend using the new WorldRoot:Raycast() since Ray.new() is deprecated.
Also to raycast it downwards simply make the direction the UpVector of the part and make it negative.
local raycastResult = workspace:Raycast(part.Position, -part.CFrame.UpVector * 10)
WorldRoot:Raycast() returns a RaycastResult, not a Ray datatype. If you did this
workspace:FindPartOnRay(raycastResult, part)
it will return an error.
So, what is RaycastResult? A RaycastResult is a returned table when the Raycast was a success. This table contains Distance, Instance, Material, Position, and Normal.
Instance is the part that the ray intersected. It will return nil if no part was found in the ray.
while true do
local raycastResult = workspace:Raycast(part.Position, -part.CFrame.UpVector * 10)
if raycastResult.Instance ~= nil then
print("Ray found a part!")
else
print("Ray hasn't found a part yet.")
end
task.wait(1)
end
Also, instead of WorldRoot:FindPartOnRayWithIgnoreList() or WorldRoot:FindPartOnRayWithWhitelist(), you can use the third parameter RaycastParams.new().
RaycastParams returns a table that lets you choose what part/s will be filtered, what filter type will the filtered part/s set to (blacklist or whitelist), whether to ignore water or not, and lets you set its collision group.
local PartA = workspace.PartA
local PartB = workspace.PartB
local PartC = workspace.PartC
local raycastParams = RaycastParams.new()
raycastParams.FilterType = Enum.RaycastFilterType.Blacklist
raycastParams.FilterDescendantsInstances = {PartA, PartB}
while true do
local raycastResult = workspace:Raycast(part.Position, -part.CFrame.UpVector * 10, raycastParams)
if raycastResult.Instance ~= nil then
print("Ray found a part!")
else
print("Ray hasn't found a part yet.")
end
task.wait(1)
end
3 comments
Thanks for commenting, but when I use that, I don't know how to detect a hit. Since when I just simply paste that over what the ray used to be, I get an error saying "Argument 1 missing or nil" on line 6. I'll just show you the code.
local part = script.Parent local rayPart = game.Workspace.RayPart local exRay = workspace:Raycast(part.Position, -part.CFrame.UpVector * 10) while wait(1) do local hit, position = game.Workspace:FindPartOnRayWithIgnoreList(exRay, {part}) if hit then part["Windows Ding"]:Play() end end
You did it wrong, WorldRoot:Raycast() doesn't return a Ray, it returns a raycastResult. I'm gonna edit my answer so you will understand.