Why am I getting this error?

Why am I getting this error?

  14:41:22.369  Workspace.StarJ3M.WallHop:18: attempt to index nil with 'Instance
local char = game.Players.LocalPlayer.Character or game.Players.LocalPlayer.CharacterAdded:Wait()
local humanoid = char:WaitForChild("Humanoid")
local root = char.PrimaryPart

local origion = root.Position
local direction = origion.Unit * Vector3.new(1, 1, 2)

local part = Instance.new("Part", workspace)
part.Anchored = true
part.Position = origion
part.Size = direction

while task.wait() do
	part.Position = origion
	part.Size = direction
	local forwardRay = workspace:Raycast(origion, direction)
	
	print(forwardRay.Instance)
end

You are not checking if forwardRay is valid so the print will attempt to index “Instance” which is a child of forwardRay which may be nil.

Because forwardRay is nil. Instead you have to check if it’s valid:

if forwardRay then
   ...
end
1 Like

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.