RayCast not detecting anything

You can write your topic however you want, but you need to answer these questions:

  1. What do you want to achieve? Keep it simple and clear!
    I want a part to detect if there is anything in front of it

  2. What is the issue? Include screenshots / videos if possible!
    It never detects anything in front of it no matter how I write the RayCast

  3. What solutions have you tried so far? Did you look for solutions on the Developer Hub?
    I’ve never had a problem with raycast like this before and I don’t know what to do

local stationPresent = false
	
	while not stationPresent do
		task.wait()
		local rayCast = Ray.new(sensor.Position, sensor.CFrame.LookVector * 1)
		local hit, position = game.Workspace:FindPartOnRayWithIgnoreList(rayCast, {sensor})

		if hit then
			sensor.Position = Vector3.new(sensor.Position.X, sensor.Position.Y, sensor.Position.Z + 18.1)
			print("hit")
		else
			print("Make a new station")
			local makeStation = game.ReplicatedStorage.makeStation:Clone()
			makeStation.Parent = workspace

			makeStation:SetPrimaryPartCFrame(CFrame.new(sensor.CFrame.Position))
			local newPosition = makeStation.PrimaryPart.Position + Vector3.new(0, 0, 8.2)
			makeStation:SetPrimaryPartCFrame(CFrame.new(newPosition) * CFrame.new(0, -1.7, 0))

			sensor.Position = sensorPosition
			stationPresent = true
		end
	end
	stationPresent = false

I recommend to use game.Workspace:Raycast() instead, as game.Workspace:FindPartOnRayWithIgnoreList() is deprecated by the Roblox Documentary. Here’s an example of how it would work:

local Params = RaycastParams.new()
Params.FilterDescendantsInstances = {script.Parent}
Params.FilterType = Enum.RaycastFilterType.Exclude
Params.IgnoreWater = true
	
local Origin = sensor.Position
local Direction = sensor.CFrame.LookVector

local RayLength = 100
	
local Raycast = game.Workspace:Raycast(Origin, Direction * RayLength, Params)

if Raycast and Raycast.Instance then
    -- do whatever
end

How would I replace this in the script?

EDIT: I replaced it inside the script, but still doesn’t detect anything, am I doing something wrong?

local stationPresent = false
	
	while not stationPresent do
		task.wait()
		local Params = RaycastParams.new()
		Params.FilterDescendantsInstances = {sensor}
		Params.FilterType = Enum.RaycastFilterType.Exclude
		
		local Origin = sensor.Position
		local Direction = sensor.CFrame.LookVector
		local rayLength = 5
		
		local Raycast = game.Workspace:Raycast(Origin, Direction * rayLength, Params)

		if Raycast and Raycast.Instance then
			sensor.Position = Vector3.new(sensor.Position.X, sensor.Position.Y, sensor.Position.Z + 18.1)
			print("hit")
		else
			print("Make a new station")
			local makeStation = game.ReplicatedStorage.makeStation:Clone()
			makeStation.Parent = workspace

			makeStation:SetPrimaryPartCFrame(CFrame.new(sensor.CFrame.Position))
			local newPosition = makeStation.PrimaryPart.Position + Vector3.new(0, 0, 8.2)
			makeStation:SetPrimaryPartCFrame(CFrame.new(newPosition) * CFrame.new(0, -1.7, 0))

			sensor.Position = sensorPosition
			stationPresent = true
		end
	end
	stationPresent = false

Your code seems fine to me. For testing purposes, try setting the raycast distance to something high, just to ensure it can and will detect something. Also, is the sensor part facing the other part you want to detect

I set it to 10 which should be enough, but still stays in the same place even though there is something in front of it

Can you send an image or a video of the issue? Everything in the code seems to work for me (for something different obviously), so it could be a placement issue or something.

robloxapp-20240526-1356030.wmv (184.7 KB)

If there is nothing present in front of it, it places a station, but if there is something in front of it, it moves in front of the station to place another one, but it just stays still

Can you check to make sure the the sensor part is facing the other part? To check, right click the sensor part, and look for “Show Orientation Indicator”. There, you make sure that the “F” is facing the part. Tell me if it is.

It works now!
Apparently it was facing the opposite direction

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.