Raycast isn't created

local handle = script.Parent:WaitForChild("Handle")

local origin = handle.Position
local direction = handle.CFrame.LookVector * 100

local raycastParams = RaycastParams.new()
raycastParams.FilterType = Enum.RaycastFilterType.Blacklist
raycastParams.FilterDescendantsInstances = {handle}

script.Parent.Activated:Connect(function()
    local result = workspace:Raycast(origin, direction, raycastParams)
    print(result)
    
    if result then
        print(result.Instance)
    end
end)

The first print statement prints nil every time and I tried using print statements after if result then and they didn’t print. So the ray isn’t being created and I’m not sure why. It’s in a local script if that means anything.

It’s printing nil because there’s nothing to hit within 100 studs, not because it isn’t being created

I am firing it at parts that are in front of me.

Have you tried checking which is the actual lookvector of your handle, (the front face). Try put a decal inside the Handle and it’ll show you which direction it is looking

Don’t know if you meant to do this but the lookvector of the handle will point where the handle is facing, not where you are clicking

1 Like

Yeah I tried doing that with a surface gui and I was using the front.

Yeah I did know that and I was using it like that, still nil.

If the handle is moving, then the origin and direction variables will need to change for the new Position and LookVector values. This may be the cause of this. If it isn’t moving, then you may be casting in the wrong direction.

Putting your code as this. It should help you debug where ray is being casted.

local handle = script.Parent:WaitForChild("Handle")

local origin = handle.Position
local direction = handle.CFrame.LookVector * 100

local raycastParams = RaycastParams.new()
raycastParams.FilterType = Enum.RaycastFilterType.Blacklist
raycastParams.FilterDescendantsInstances = {handle}

local debugPart = Instance.new("Part") do
	debugPart.Anchored = true
	debugPart.CanCollide = false
	debugPart.CanTouch = false
	debugPart.CanQuery = false
	debugPart.Transparency = 0.5
	debugPart.Color = Color3.new(1,0,0)
	debugPart.Size = Vector3.new(1,1,1)
	debugPart.Parent = workspace
end

script.Parent.Activated:Connect(function()
	origin = handle.Position
	direction = (handle.CFrame.LookVector * 100)
	
	local result = workspace:Raycast(origin, direction, raycastParams)
	
	local endPos = (origin + direction)
	if result then
		print(result.Instance)
		endPos = result.Position
	end
	
	local lookAt = CFrame.lookAt(origin, endPos)
	local distance = (origin - endPos).Magnitude

	debugPart.CFrame = lookAt:Lerp(lookAt + (direction.Unit * distance), 0.5)
	debugPart.Size = Vector3.new(1,1,distance)
end)

EDIT: Major mistake, accidentally had a + operator when it was supposed to be a * operator. Should be fixed now.

EDIT 2: changed cframe lerp goal.

1 Like

Try visualizing the raycast, even if it isn’t hitting anything.

That’s what the code I provided is supposed to do.

rays can’t be created in a local script I think? I tried it before and it didn’t work for me as well

They can? You’re just using localscripts wrong.


I have no idea what is even happening here. Anyways putting the variables in the .Activated event fixed it, probably because they needed to be updated every time.

You’re using the lookvector of the part? What’s wrong with it?

oh, I’m new to raycasting as well so I don’t fully understand them either

I tested the code and made a few errors. I fixed those, so it should work now.

If you were referring to how the part is showing the ray rather than the messed up placement, it works by retrieving the end position, creating a cframe at the origin position and “looking” (has a lookvector pointed at) at the end position, and then getting the position by getting the midpoint between the origin and end positions. It also sets the size on the zAxis (LookVector axis) to the distance between the origin and end positions.

Shouldn’t origin and direction be determined when the tool’s “.Activated” event fires? Otherwise you’d just be using the tool’s handle’s initial origin and lookvector values.

Never mind, I see this has been suggested.