Workspace.Part.Scripts:5: attempt to index nil with 'Position' on raycast

You can write your topic however you want, but you need to answer these questions:

  1. What do you want to achieve? Keep it simple and clear!
    Hello so i was making apart that shoots a ray cast from its positions and ends 20 studs away (i was making this for a shooter type of thing but when i want to test it gave me the errorWorkspace.Part.Scripts:5: attempt to index nil with ‘Position’
  2. What is the issue? Include screenshots / videos if possible!
    it worked before but i moved it to a model and it gave me this error after moving it to a model it putted it in workspace and it doenst work anymore how can i fix this? heres the script
  3. What solutions have you tried so far? Did you look for solutions on the Developer Hub?
-- local function shoot()
	local origin = script.Parent.Position
	local direction = script.Parent.Position + script.Parent.CFrame.LookVector*100
	local result = workspace:Raycast(origin, direction)
		local distance = (origin - result.Position).Magnitude
		local p = Instance.new("Part")
		p.Anchored = true
	p.CanCollide = false
	p.Parent = workspace
		p.Size = Vector3.new(0.1, 0.1, distance)
	p.CFrame = CFrame.lookAt(origin, direction)*CFrame.new(0, 0, -distance/2)
	wait(1)
	p:Destroy()
end
while true do
	wait(1)
	shoot()
end

“the error is at the line 5”

Result on Raycast is nil if it hits nothing. In that case the hit position is your origin + direction.

1 Like