Ray cast not working

I’m just trying to learn this so don’t criticize me for this… Anyway here is the script, it is a child of a tool

script.Parent.Activated:Connect(function()
    local ray = Ray.new(script.Parent.Part.CFrame.Position, script.Parent.Part.CFrame.LookVector * 100)
    print(ray.Direction)
end)

i don’t see anything necessarily wrong with your code, Are there any errors? or are you expecting something else to happen, beside the ray’s direction printing to the output?

There are no errors, and the direction is not printing. And how would you make the ray visible

Never mind I fixed by adding a handle (Which I forgot to do) but how would you make the ray visible?

The easiest way to visualize a ray is by instancing a part:

local function visualizeRay(ray)
	local part = Instance.new("Part")
	part.Size = Vector3.new(1, 1, ray.Direction.Magnitude)
	part.CFrame = CFrame.new(
		ray.Origin + ray.Direction/2, 
		ray.Origin + ray.Direction
	)
	part.Anchored = true
	part.CanCollide = false
	part.Parent = workspace
	
	coroutine.wrap(function()
		wait(5)
		part:Destroy()
	end)()
end
1 Like

Ok but I want to learn, so could you teach me about the raycast stuff? (I already know the other stuff in that script, I just need to learn the raycasting parts)

Raycasting is already a well-documented subject on the dev hub:

https://developer.roblox.com/en-us/api-reference/datatype/Ray

A Ray is a half-line, finite in one direction but infinite in the other. It can be defined by a 3D point, where the line originates from, and a direction vector, which is the direction it goes in.

You can identify BaseParts intersecting a Ray using Workspace:FindPartOnRay and its alternatives, for more information see Workspace.

There’s also a tutorial for raycasting; it uses the brand new :Raycast method instead of :FindPartOnRay, but it doesn’t matter which method to use (at the moment).