Why isn't my script working?

So this script isn’t working:

local networkowner = 0
local autonetwork = false
script.Parent.NetworkOwnerShip.OnServerEvent:Connect(function(player, part, set)
	if player.UserId == game:GetService('Players'):GetPlayerFromCharacter(script.Parent.Parent).UserId then
		if set then
			networkowner = part:GetNetworkOwner().UserId
			autonetwork = part:GetNetworkOwnershipAuto()
			print(networkowner, autonetwork)
			spawn(function()
				repeat wait() part:SetNetworkOwner(player) until networkowner == nil -- Prevent false actions
			end)
		else
			task.wait()
			if autonetwork then
				part:SetNetworkOwnershipAuto()
			else
				part:SetNetworkOwner(game:GetService('Players'):GetPlayerByUserId(networkowner))
			end
			task.wait()
			autonetwork = false
			networkowner = 0
		end
	end
end)


script.Parent.Beam.OnServerEvent:Connect(function(player, part, pos, bool)
	if player.UserId == game:GetService('Players'):GetPlayerFromCharacter(script.Parent.Parent).UserId then
		local a1 = Instance.new('Attachment')
		if part then
			a1.Parent = part
		end
		a1.WorldPosition = pos
		script.Parent.MeshPart.Beam.Attachment1 = a1
		script.Parent.Deactivated:Wait()
		a1:Destroy()
		script.Parent.MeshPart.Beam.Attachment1 = nil
	end
end)

And this is what error it is:

part doesn’t exist

this is a basic Lua error

Huh? But the part is in line 3 code.

What? I fixed it, but doesn’t work and no errors.

local networkowner = 0
local autonetwork = false
local partobject
script.Parent.NetworkOwnerShip.OnServerEvent:Connect(function(player, part, set)
	if player.UserId == game:GetService('Players'):GetPlayerFromCharacter(script.Parent.Parent).UserId then
		if set then
			partobject = part
			autonetwork = part:GetNetworkOwnershipAuto()
			networkowner = part:GetNetworkOwner().UserId
			if part:GetNetworkOwner() then
				autonetwork = false
			end
			print(networkowner, autonetwork)
			spawn(function()
				repeat wait() part:SetNetworkOwner(player) until networkowner == nil -- Prevent false actions
			end)
		else
			task.wait()
			if autonetwork then
				partobject:SetNetworkOwnershipAuto()
			else
				partobject:SetNetworkOwner(game:GetService('Players'):GetPlayerByUserId(networkowner))
			end
			task.wait()
			autonetwork = false
			networkowner = 0
			partobject = nil
		end
	end
end)


script.Parent.Beam.OnServerEvent:Connect(function(player, part, pos, bool)
	if player.UserId == game:GetService('Players'):GetPlayerFromCharacter(script.Parent.Parent).UserId then
		local a1 = Instance.new('Attachment')
		if part then
			a1.Parent = part
		end
		a1.WorldPosition = pos
		script.Parent.MeshPart.Beam.Attachment1 = a1
		script.Parent.Deactivated:Wait()
		a1:Destroy()
		script.Parent.MeshPart.Beam.Attachment1 = nil
	end
end)