RayValue

Show Deprecated

A RayValue is an object whose purpose is to store a single Ray. Similar to CFrameValue, a RayValue's stored ray cannot be viewed or edited within the Properties window within studio. Instead, use the Command bar to get and set the value of these objects. For example, you can use a line like the one below to create a new RayValue named "Value" within the Workspace. It creates a ray at (0, 50, 0) and it faces in the positive-X direction.

Instance.new("RayValue").Value = Ray.new(Vector3.new(0, 50, 0), Vector3.new(10, 0, 0))

Since there is no trivial way to edit rays within Studio, sometimes it is better to use a CFrameValue instead (which can be changed through a part or the camera). You can reconstruct a ray from a CFrame using Ray.new(cf.p, cf.lookVector * dist), where cf is a given CFrame and dist is the length of the Ray you want to construct.

Like all "-Value" objects, this single value is stored in the Value property. The Changed event for this (and other objects like it) will fire with the new value being stored in the object, instead of a string representing the property being changed.

Code Samples

Rays, RayValue and Raycasting

1local partA = workspace.PartA
2local partB = workspace.PartB
3
4local origin = partA.Position
5local direction = partB.Position - partA.Position
6local ray = Ray.new(origin, direction)
7
8local rayValue = Instance.new("RayValue")
9rayValue.Value = ray
10rayValue.Name = "PartA-to-PartB Ray"
11rayValue.Parent = workspace
12
13-- Raycast to find any parts in between PartA and PartB
14local part = workspace:FindPartOnRay(ray)
15if part then
16 print("Hit part: " .. part:GetFullName())
17else
18 -- This ought to never happen, as our ray starts at PartA
19 -- and points towards PartB, so we should always hit PartB
20 -- or some other part.
21 print("No part hit!")
22end

Summary

Properties

The stored Ray.

Events


Fired when RayValue.Value is changed.

Methods

Properties

Value

The stored Ray.

Events

Changed

This event fires whenever the RayValue.Value property is changed. It will run with the new value being stored in the argument object, instead of a string representing the property being changed.

This event, like other changed events, can be used to track when an RayValue changes and to track the different values that it may change to.

Equivalent changed events exist for similar objects, such as NumberValue and StringValue, depending on what object type best suits the need.

Parameters

value: Ray

The value after the change.


Code Samples

How to Use RayValue.Changed

1local value = Instance.new("RayValue")
2value.Parent = workspace
3
4value.Changed:Connect(function(NewValue)
5 print(NewValue)
6end)
7
8local start = Vector3.new(0, 0, 0)
9local lookAt = Vector3.new(10, 10, 10)
10local ray = Ray.new(start, (lookAt - start).Unit)
11value.Value = ray

Methods