Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
✓ ANSWER ACCEPTED

Finding the ending point of a raycast?

local RANGE = 5

local ray = Ray.new(Player.Character.HumanoidRootPart.Position, Player.Character.HumanoidRootPart.CFrame.lookVector * RANGE)

local part, endPoint = workspace:FindPartOnRay(ray, Player.Character)				
					
BrickModel.Parent = workspace
					
BrickModel:SetPrimaryPartCFrame(part.CFrame * CFrame.new(0, 0, 0))

This script is meant to spawn BrickModel where the RayCast hits but it gives this error:

Players.Player.Backpack.BrickPlacer:10: attempt to index local 'part' (a nil value) Script 'Players.Player.Backpack.BrickPlacer', Line 10

NOTE: I edited the error to match the lines of code in this question.

Please Help.

1 answer

✓ Accepted answer

If the ray doesn't hit anything it will return nil instead of part, and the position it ended at(it depends on your set starting point, direction and length of direction vector).

local RANGE = 5

local ray = Ray.new(Player.Character.HumanoidRootPart.Position, Player.Character.HumanoidRootPart.CFrame.lookVector * RANGE)

local part, endPoint = workspace:FindPartOnRay(ray, Player.Character)               

BrickModel.Parent = workspace

BrickModel:SetPrimaryPartCFrame(CFrame.new( endPoint ))

This discussion is preserved as it was. New answers, comments, and votes are closed.