Syntax
local hit = workspace:Raycast(origin, direction, parameters)Examples
Find a surface below a point
Script in ServerScriptService in a baseplate place. The ray starts twenty studs up and searches thirty studs down. It prints the result only when a surface is found.
local origin = Vector3.new(0, 20, 0)
local direction = Vector3.new(0, -30, 0)
local hit = workspace:Raycast(origin, direction)
if hit then
print(hit.Instance.Name, hit.Position, hit.Normal)
else
print("No surface within range")
endExclude your character
LocalScript in StarterPlayerScripts. This is a local observation for presentation, not authoritative evidence of a hit. A competitive action needs server-side validation of origin, range and game state.
local Players = game:GetService("Players")
local character = Players.LocalPlayer.Character or Players.LocalPlayer.CharacterAdded:Wait()
local root = character:WaitForChild("HumanoidRootPart")
local parameters = RaycastParams.new()
parameters.FilterType = Enum.RaycastFilterType.Exclude
parameters.FilterDescendantsInstances = {character}
local hit = workspace:Raycast(root.Position, root.CFrame.LookVector * 40, parameters)
if hit then print(hit.Instance:GetFullName()) endBest practices
- Multiply a normalized direction by the intended range.
- Handle nil before reading hit.Position or hit.Instance.
- Streaming can make a client’s available geometry differ from the server’s; do not treat the client query as authoritative.
At a glance
- Purpose
- Typed scripting and Roblox development
- File extension
- .luau
- Runs in
- Luau host; Roblox engine examples require Roblox Studio
- Usually used with
- Roblox APIs and Studio
Specifications & further reading
Related Luau documentation
Server authority, validation and rate limits
A valid-looking remote call can still be an impossible game action. Validate both the shape of a request and whether the sender may perform it now. This example lets a nearby living player request a visual switch; it does not trust a client position, target Instance or reward.Humanoid, HumanoidRootPart and attributes
Humanoid controls character behaviour such as health, while HumanoidRootPart gives a useful spatial reference. Attributes attach small named values to Instances. They are convenient for configuration and display, but client-side attributes are not proof that a player earned a reward.Instances, properties and methods
Instances form the Roblox object tree. Properties describe an object, methods perform operations, and events report changes. Check the class before using class-specific properties; a child with the right name can still be the wrong kind of object.Touch events, proximity prompts and debounce
Touched reports physical contact and can fire repeatedly for different body parts. ProximityPrompt presents an intentional interaction. Both need server checks for valuable actions, and cooldown state should match the unit you intend to limit: an object, a player or an action.
A valid-looking remote call can still be an impossible game action. Validate both the shape of a request and whether the sender may perform it now. This example lets a nearby living player request a visual switch; it does not trust a client position, target Instance or reward.Humanoid, HumanoidRootPart and attributes
Humanoid controls character behaviour such as health, while HumanoidRootPart gives a useful spatial reference. Attributes attach small named values to Instances. They are convenient for configuration and display, but client-side attributes are not proof that a player earned a reward.Instances, properties and methods
Instances form the Roblox object tree. Properties describe an object, methods perform operations, and events report changes. Check the class before using class-specific properties; a child with the right name can still be the wrong kind of object.Touch events, proximity prompts and debounce
Touched reports physical contact and can fire repeatedly for different body parts. ProximityPrompt presents an intentional interaction. Both need server checks for valuable actions, and cooldown state should match the unit you intend to limit: an object, a player or an action.