I am making a system that the train detects the nearest semaphore and it is detected by ray and the problem is that if something gets on the tracks, for example there is a station, and after the station there is the semaphore itself, then ray will ignore the semaphore. Here is the code:
function detectNearestLight()
local seat = script.Parent.Floor
local rayOrigin = seat.Position
local rayDirection = seat.CFrame.LookVector * distance -- Adjust the length of the ray as needed
local raycastParams = RaycastParams.new()
raycastParams.FilterDescendantsInstances = {script.Parent.Parent}
raycastParams.FilterType = Enum.RaycastFilterType.Blacklist
local raycastResult = workspace:Raycast(rayOrigin, rayDirection, raycastParams)
if raycastResult then
local hitPart = raycastResult.Instance
if hitPart then
if hitPart.Name == "trainlightdetector" then
if player then
if player.PlayerGui:FindFirstChild("GuiS") then
local remote = player.PlayerGui.GuiS.RemoteEvent:FireClient(player,"NearestLight",getdistance(seat.Position,raycastResult.Position),tostring(hitPart.LightColor.Value))
end
end
end
end
end
end