local module = {}

local function returnNetworkOwner(this)
	local owner = this:GetAttribute("NetworkOwner") or this.Parent:GetAttribute("NetworkOwner")
	if owner then owner = game.Players:GetPlayerByUserId(owner) end
	this:SetNetworkOwner(owner) -- there is a bug here?
end

function module.init(this)
	do return end
	this.Parent.Changed:Connect(function(name)
		if name == "Occupant" then
			if this.Parent.Occupant then
				local player = game.Players:GetPlayerFromCharacter(this.Parent.Occupant.Parent)
				if player then
					if this.Parent.Parent:HasTag("P_"..player.UserId) or true then
						this.Parent:SetNetworkOwner(player)
						--print("Vehicle ownership set to "..player.DisplayName)
					end
				else
					returnNetworkOwner(this.Parent)
				end
			else
				returnNetworkOwner(this.Parent)
			end
		end
	end)
end

return module
