Fire an invisible ray from any point and read exactly what it hits — the tool behind line of sight, ground checks, and laser beams.
"Is this wall between me and the ? What's under my feet? Did my laser hit the target?" Games answer those questions with a : an invisible ray fired from a point in a direction, with Roblox reporting back the first thing it hits.
A ray needs two things: a starting point and a direction. Fire it and Roblox tells you what it hit:
local origin = Vector3.new(0, 5, 0) -- where the ray starts
local direction = Vector3.new(0, 0, -50) -- which way it goes, and how far (50 studs)
local result = workspace:Raycast(origin, direction)
origin — a position the ray leaves from.direction — a whose length is the max distance the ray travels.nil if the ray hit nothing.A ray travels its full distance without touching anything. What does workspace:Raycast return?
When the ray hits, the result tells you everything about the impact:
if result then
print(result.Instance) -- the part the ray struck
print(result.Position) -- the exact point it landed on
print(result.Normal) -- which way the surface faces
end
Three fields, three answers:
result.Instance — what was hit (the ).result.Position — where the ray landed (a ).result.Normal — which way that surface points, useful for placing things flush against it.The ray hit a wall. To find out which part it hit, read on the result.
A raw ray hits everything in its path — including the who fired it. Filter it with RaycastParams so you don't shoot yourself:
local rayParams = RaycastParams.new()
rayParams.FilterType = Enum.RaycastFilterType.Exclude
rayParams.FilterDescendantsInstances = { player.Character }
local result = workspace:Raycast(origin, direction, rayParams)
FilterType.Exclude — "hit everything except the list below."FilterDescendantsInstances — the to ignore (your own character model).Raycast.Cast the ray with rayParams so it ignores the player's own character. Write the line.
local rayParams = RaycastParams.new()
rayParams.FilterType = Enum.RaycastFilterType.Exclude
rayParams.FilterDescendantsInstances = { player.Character }
-- cast the filtered ray
Cast a ray straight down from the character and you can read the height of the floor below — for landing checks, footsteps, or keeping a camera off the ceiling:
local rayParams = RaycastParams.new()
rayParams.FilterType = Enum.RaycastFilterType.Exclude
rayParams.FilterDescendantsInstances = { character }
local origin = character.HumanoidRootPart.Position
local direction = Vector3.new(0, -100, 0) -- straight down, up to 100 studs
local result = workspace:Raycast(origin, direction, rayParams)
if result then
print("ground at", result.Position.Y)
end
Downward rays are the same recipe as any other ray — the trick is that the direction points down, and the filter keeps the ray from hitting the character itself.
Write the direction vector that points straight down, so the ray checks for ground below.
local origin = character.HumanoidRootPart.Position
-- point the ray straight down
nil — result.Instance on a miss crashes. Always guard with if result then.workspace:Raycast(origin, workspace.Target.Position) fires a ray to a point far away, not toward the target. The direction must be a from the origin.RaycastParams is fine for one cast. If different casts need different filters, make a new one — or rebuild the list.local origin = Vector3.new(0, 5, 0)
local direction = workspace.Target.Position -- a position, not a direction!
local result = workspace:Raycast(origin, direction)
Type the corrected direction line — a ray's direction must point from the origin toward the target.
Next: a bug hunt across the whole world — bugs included.