Development / ROBLOX
Why Raycast returns nil in Roblox Studio
Prove a hit in a simple scene, then isolate direction, filters, CanQuery, and client streaming one at a time.
Build a test that must hit #
In an empty test place, put an anchored block in Workspace. Start ten studs above its center and point twenty studs down. Run the query without custom filters and read Output; the returned instance should be your block. Move the origin to one side for a deliberate miss so you know what both outcomes look like before you bring back the real scene.
local block = workspace:FindFirstChild("RayProbe")
assert(block and block:IsA("BasePart"), "Name your test block RayProbe")
local origin = block.Position + Vector3.new(0, 10, 0)
local result = workspace:Raycast(origin, Vector3.new(0, -20, 0))
if result then
print(result.Instance.Name, result.Distance, result.Position)
else
print("No hit", origin)
endInspect the actual endpoint #
The first Raycast vector is a world position and the second is an offset. If you have two points, subtract the origin from the destination. Passing a Unit vector by itself makes a one-stud ray. Mark origin + direction with a temporary visual guide and check whether that endpoint reaches the block before increasing your range.
Add filtering back one condition at a time #
Check that an Include filter covers the target or an ancestor. With Exclude, make sure the target is not inside an excluded folder. Inspect the collision group and IgnoreWater if you are testing terrain water. Repeat the same query after each change and record the first object it sees, since a nearer part may be the actual problem.
Separate visibility from query eligibility #
A transparent part can intercept a ray. A visible part can be excluded with CanQuery; Roblox requires CanCollide to be disabled for that property to take effect. RespectCanCollide changes which property the ray uses. If one tool should ignore a decoration, first try a filter on that query instead of changing the part for every gameplay system.
Compare a nearby and distant client test #
With StreamingEnabled, a distant part may not have been loaded on the client. Compare the same world origin and direction on the client and server, then try again close to the target. A difference confined to distance suggests a streaming question. A server hit alone cannot prove the client already has the same geometry.
Make misses an ordinary result #
Record Instance, Distance, and Position on a hit. On nil, record origin, direction, and filter mode. Guard the result before reading Instance. Your regression check should include a known hit, a deliberate miss, and a ray that misses only when your specific exclusion is enabled.
| What to check | Expected result |
|---|---|
| Test block | Block is hit |
| Ray endpoint | Reaches the target |
| Filter | Target is included |
| CanQuery | Part is eligible |
| Nearby test | Compare with server |
| Miss | Handle nil |
Original sources
Roblox Creator Hub · RaycastingRoblox API · RaycastParams
Roblox API · CanQuery

