Raycasting not scanning filtered part

I am trying to get the ray to only allow parts hit with the name “TrainSensor” but it’s not working.

Screen Shot 2021-05-30 at 12.59.29 PM

ignore = {script.Parent}

function Sensor1()
 local ray = Ray.new(script.Parent.Sensor.Position, script.Parent.Sensor.CFrame.RightVector * 0 + script.Parent.Sensor.CFrame.lookVector * 3500 + script.Parent.Sensor.CFrame.UpVector * 0)
 local hit, position = workspace:FindPartOnRayWithIgnoreList(ray,ignore)
 print(hit)
 if hit.Name == "TrainSensor" then
 	script.Parent.Sensor.InUse.Value = true
 else
 	script.Parent.Sensor.InUse.Value = false
 end
end

function Sensor2()
 local ray = Ray.new(script.Parent.Sensor2.Position, script.Parent.Sensor2.CFrame.RightVector * 0 + script.Parent.Sensor2.CFrame.lookVector * 3500 + script.Parent.Sensor2.CFrame.UpVector * 0)
 local hit, position = workspace:FindPartOnRayWithIgnoreList(ray,ignore)
 if hit.Name == "TrainSensor" then
 	script.Parent.Sensor2.InUse.Value = true
 else
 	script.Parent.Sensor2.InUse.Value = false
 end
end

while true do
 wait(1)
 for i = 1,10 do
 	Sensor1()
 	Sensor2()
 end
end

Just a side note, but FindPartOnRayWithIgnoreList is deprecated

I believe it may be due to just adding a sanity check, to make sure that there’s a valid hit variable

Of course you can use the workspace:Raycast method as well:

local RayParams = RaycastParams.new()
RayParams.FilterType = Enum.RaycastFilterType.Blacklist --This is the same as ignore list
RayParams.FilterDescendantsInstances = {ignore}

function Sensor1()
    local Origin = script.Parent.Sensor
    local Direction = (Origin.Position + Origin.CFrame.LookVector).Unit * 3500
    local Result = workspace:Raycast(Origin, Direction, RayParams)
 
    if Result then
        print(Result.Instance)

        if Result.Instance == "TrainSensor" then
            Origin.InUse.Value = true
        else
            Origin.InUse.Value = false
        end
    end
end

function Sensor2()
    local Origin = script.Parent.Sensor2
    local Direction = (Origin.Position + Origin.CFrame.LookVector).Unit * 3500
    local Result = workspace:Raycast(Origin, Direction, RayParams)
 
    if Result then
        print(Result.Instance)

        if Result.Instance == "TrainSensor" then
            Origin.InUse.Value = true
        else
            Origin.InUse.Value = false
        end
    end
end

while true do
    wait(1)
    for i = 1,10 do
 	    Sensor1()
 	    Sensor2()
    end
end

Try this perhaps?

Screen Shot 2021-05-30 at 1.11.07 PM

Whoops, forgot to set a position

Try this:

local RayParams = RaycastParams.new()
RayParams.FilterType = Enum.RaycastFilterType.Blacklist --This is the same as ignore list
RayParams.FilterDescendantsInstances = {ignore}

function Sensor1()
    local Origin = script.Parent.Sensor
    local Direction = (Origin.Position + Origin.CFrame.LookVector).Unit * 3500
    local Result = workspace:Raycast(Origin.Position, Direction, RayParams)
 
    if Result then
        print(Result.Instance)

        if Result.Instance == "TrainSensor" then
            Origin.InUse.Value = true
        else
            Origin.InUse.Value = false
        end
    end
end

function Sensor2()
    local Origin = script.Parent.Sensor2
    local Direction = (Origin.Position + Origin.CFrame.LookVector).Unit * 3500
    local Result = workspace:Raycast(Origin.Position, Direction, RayParams)
 
    if Result then
        print(Result.Instance)

        if Result.Instance == "TrainSensor" then
            Origin.InUse.Value = true
        else
            Origin.InUse.Value = false
        end
    end
end

while true do
    wait(1)
    for i = 1,10 do
 	    Sensor1()
 	    Sensor2()
    end
end