I’m trying to raycast to detect a certain part, but no matter what I do, it always returns nil.
Code:
function module.CheckConveyor(detector, distance, blacklist)
local cS = game:GetService(“CollectionService”)
local whiteListedObjects = {}
local raycastParams = RaycastParams.new()
for i, v in ipairs(workspace.Base.ItemHolder:GetChildren()) do
if detector.Parent ~= v then
whiteListedObjects[#whiteListedObjects + 1] = v
end
end
raycastParams.FilterDescendantsInstances = {table.unpack(whiteListedObjects)}
raycastParams.FilterType = Enum.RaycastFilterType.Whitelist
--print(detector.CFrame.LookVector)
--print(detector.Parent)
local raycastResult = workspace:Raycast(detector.Position, detector.CFrame.LookVector*distance, raycastParams)
--print(raycastResult)
if raycastResult then
local hit = raycastResult.Instance
if cS:HasTag(hit, "ConveyorEntrance") then
return hit
end
end
end
if cS:HasTag(child, "Conveyor") then
local hit = raycastModule.CheckConveyor(detector, distance, script.Parent)
if hit then
connection = hit
end
print(hit)
end
The ray is pointing in the right direction, and the distance should be long enough.
Video
This had caused me to bang my head against a wall for 3 days.
What part are you trying to see if it hits guessing its anything in this folder except the detectors parent?
workspace.Base.ItemHolder
and you should try some prints on your raycast results
and a few more through your script until you find where it isn’t working
I would print like this
print(raycastResult)
if raycastResult then
local hit = raycastResult.Instance
print(hit)
print(cS:HasTag(hit, "ConveyorEntrance"))
if cS:HasTag(hit, "ConveyorEntrance") then
return hit
end
end
something else you can do is setup a brick to go from your start of your ray to the end to show you where it is actually being checked
When I printed the vector, it showed (0, 0 , 1), and I’m fairly certain that that’s the correct direction. Also, raycast result is always nil, so knowing what hit is wouldn’t really help me.
Also I’m using the look vector of the object, and the front is pointing the right way
and then that should go without filtering anything
Place a block in front and see if the raycast returns something
from what it looks like you are firing the raycast from the center of the block you have highlighted and forward from there which wouldn’t hit anything in that picture
it would need to be lower firing from the bottom part of that block
The distance value is 10, and that should be more than enough length. The screenshot of the part with the decal is the detector. The look vector is (0, 0, 1
)
Update: For some reason, it detected a part in front of the detector in the 2nd model, so the ray somehow pierced the conveyor detector on the second model.