Attempt to Index Nil with Position

I’m trying to fix my shell collisions but it keeps giving me this error message when It doesn’t work…


any help?

local canexplode = true
local function explode()
	local explosion = Instance.new("Explosion")
	script.Parent.Anchored = true
	explosion.Position = script.Parent.Position
	explosion.Parent = game.Workspace
	explosion.BlastRadius = 100
	explosion.Visible = true
	local soundeffectpart = Instance.new("Part")
	soundeffectpart.Parent = game.Workspace
	soundeffectpart.Transparency = 1
	soundeffectpart.CanCollide = false
	soundeffectpart.Anchored = true
	soundeffectpart.Position = script.Parent.Position
	local sfx = Instance.new("Sound")
	sfx.SoundId = "rbxassetid://597291504"
	sfx.Volume = 5
	sfx.Parent = soundeffectpart
	sfx.RollOffMaxDistance = 100000
	sfx:Play()
	local fx = script.Parent.HitParticles:Clone()
	local fx2 = script.Parent.HitParticles2:Clone()
	script.Parent.Transparency = 1
	fx2.Parent = soundeffectpart
	fx.Parent = soundeffectpart
	fx.Enabled = true
	fx2.Enabled = true
	wait(1)
	fx2.Enabled = false
	wait(.5)
	fx.Enabled = false
	
	wait(2)
	
	soundeffectpart:Destroy()
end





while wait() do
	local ray1 = Ray.new(script.Parent.Position, Vector3.new(0,-4,0))
	local hit, position = game.Workspace:FindPartOnRayWithIgnoreList(ray1, {script.Parent})
	if hit then
		if hit.Position.Magnitude - script.Parent.Position.Magnitude < 1 and canexplode == true then
			print("explode")
			canexplode = false
			explode()
		end
	end
	local ray2 = Ray.new(script.Parent.Position, Vector3.new(0,4,0))
	local hit2, position = game.Workspace:FindPartOnRayWithIgnoreList(ray2, {script.Parent})
	if hit2 then
		if hit.Position.Magnitude - script.Parent.Position.Magnitude < 1 and canexplode == true then
			print("explode")
			canexplode = false
			explode()
		end
		
	end
	local ray3 = Ray.new(script.Parent.Position, Vector3.new(4,0,0))
	local hit3, position = game.Workspace:FindPartOnRayWithIgnoreList(ray3, {script.Parent})
	if hit3 then
		if hit.Position.Magnitude - script.Parent.Position.Magnitude < 1 and canexplode == true then
			print("explode")
			canexplode = false
			explode()
			wait(3)
			canexplode = true
		end	
	end
	local ray4 = Ray.new(script.Parent.Position, Vector3.new(-4,0,0))
	local hit4, position = game.Workspace:FindPartOnRayWithIgnoreList(ray4, {script.Parent})
	if hit then
		if hit.Position.Magnitude - script.Parent.Position.Magnitude < 1 and canexplode == true then
			print("explode")
			canexplode = false
			explode()
		end
	end
	local ray5 = Ray.new(script.Parent.Position, Vector3.new(0,0,4))
	local hit5, position = game.Workspace:FindPartOnRayWithIgnoreList(ray5, {script.Parent})
	if hit then
		if hit.Position.Magnitude - script.Parent.Position.Magnitude < 1 and canexplode == true then
			print("explode")
			canexplode = false
			explode()
		end
	end
	local ray6 = Ray.new(script.Parent.Position, Vector3.new(0,0,-4))
	local hit6, position = game.Workspace:FindPartOnRayWithIgnoreList(ray5, {script.Parent})
	if hit then
		if hit.Position.Magnitude - script.Parent.Position.Magnitude < 1 and canexplode == true then
			print("explode")
			canexplode = false
			explode()
		end
	end
end	

Thanks,

–Luke

I believe it’s caused by the raycast itself not hitting anything, but I haven’t used that version of raycasting in a while.

I figured it out, I had my hit variables not set to their according number

For example:

local ray5 = Ray.new(script.Parent.Position, Vector3.new(0,0,4))
	local hit5, position = game.Workspace:FindPartOnRayWithIgnoreList(ray5, {script.Parent})
	if hit then --should've been if hit5 then
		if hit.Position.Magnitude - script.Parent.Position.Magnitude < 1 and canexplode == true then
			print("explode")
			canexplode = false
			explode()
		end
	end