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