Parenting an accessory out of character without breaking it

So I need help parenting my accessory out of my character to a different location for storage but when parenting it back to the character, most welds would be disabled. How can I fix this?

Equipping: (server)

local ServerStorage = game:GetService("ServerStorage")
local Items = ServerStorage:WaitForChild("Items")

local ReplicatedStorage = game:GetService("ReplicatedStorage")
local Remotes = ReplicatedStorage:WaitForChild("Remotes")

local function UnequipAllItems(player)
	local ItemsStorage = player:WaitForChild("ItemsStorage")
	local character = player.Character
	local humanoid = character:FindFirstChildOfClass("Humanoid")
	
	if humanoid then
		for i,v in ipairs(humanoid:GetAccessories()) do
			if v:IsA("Accessory") and Items:FindFirstChild(v.Name) then
				local hasItem = ItemsStorage:FindFirstChild(v.Name)

				if not hasItem then
					v.Parent = ItemsStorage
				else
					v:Destroy()
				end
			end
		end
	end
end

local function EquipItem(player, itemName)
	UnequipAllItems(player)
	
	local EquippedItem = player:WaitForChild("EquippedItem")
	local ItemsStorage = player:WaitForChild("ItemsStorage")
	local hasItem = ItemsStorage:FindFirstChild(itemName)

	local character = player.Character
	local humanoid = character:FindFirstChildOfClass("Humanoid")

	if not hasItem then
		local item = Items:FindFirstChild(itemName)

		if item and humanoid then
			local newItem = item:Clone()
			humanoid:AddAccessory(newItem)
			
			EquippedItem.Value = newItem
		end
	else
		hasItem.Parent = character
		EquippedItem.Value = hasItem
	end
end

Remotes.EquipItem.OnServerEvent:Connect(function(player, itemName)
	EquipItem(player, itemName)
end)

Unequip: (server)

local ServerStorage = game:GetService("ServerStorage")
local Items = ServerStorage:WaitForChild("Items")

local ReplicatedStorage = game:GetService("ReplicatedStorage")
local Remotes = ReplicatedStorage:WaitForChild("Remotes")

local function UnequipItem(player, itemName)
	local EquippedItem = player:WaitForChild("EquippedItem")
	local ItemsStorage = player:WaitForChild("ItemsStorage")
	local character = player.Character
	local humanoid = character:FindFirstChildOfClass("Humanoid")

	if humanoid then
		for i,v in ipairs(humanoid:GetAccessories()) do
			if v:IsA("Accessory") and v.Name == itemName and Items:FindFirstChild(v.Name) then
				v.Parent = ItemsStorage
				EquippedItem.Value = nil
			end
		end
	end
end

Remotes.UnequipItem.OnServerEvent:Connect(function(player, itemName)
	UnequipItem(player, itemName)
end)
1 Like