Scripting Helpers is winding down operations and is now read-only. More info→
Ad
Log in to vote
1

Finding the ending point of a raycast?

Asked by 8 years ago
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

Log in to vote
2
Answered by 8 years ago

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 ))
Ad

Answer this question