Ive made a script to equip items to the back of your torso. What should i improve?

Ive made a script to equip items to the back of your torso. What should i improve?
Here is my code. Ive used a remote event (client to server) that fires after the “equip” button is clicked.

Client:

local player = game.Players.LocalPlayer
local rep = game:GetService("ReplicatedStorage")

local unequipped = true
local children = script.Parent.Parent.Items:GetChildren() --searching the items folder which only contains textbuttons
for _, item in pairs(children) do

	item.MouseButton1Click:Connect(function()
		script.Parent.TextLabel.Text = item.Name
		script.Parent.Equip.Visible = true

		local function equipitem()
			print("equipped")
			script.Parent.Equip.Text = "Unequip"
			local itemPart = rep:WaitForChild("Items").Item01
			rep:WaitForChild("Events").ItemEquip:FireServer(itemPart)
			
		end

		local function unequipitem()
			script.Parent.Equip.Text = "Equip"
			rep:WaitForChild("Events").ItemUnequip:FireServer()
			print("unequipped")
			
		end

		script.Parent.Equip.MouseButton1Click:Connect(function()
			if unequipped == true then
				unequipped = false
				equipitem()
			else
				unequipitem()
				unequipped = true
			end
		end)

	end)

end

Server:

local runservice = game:GetService("RunService")
local rep = game:GetService("ReplicatedStorage")
local itemClone
local itemToequip
local itemTodisable
local function itemequip(player, item)
	local character = workspace:FindFirstChild(player.Name)
	itemClone = item:Clone()
	itemClone.Parent = character
	local weld = Instance.new("WeldConstraint", itemClone)
	weld.Part0 = itemClone
	weld.Part1 = character.HumanoidRootPart
	itemClone.Position = (character.HumanoidRootPart.Position) - Vector3.new(0, 0, -0.83084750175476)
	
	print("itemdisconnect")
end

local function itemunequip()
	itemClone:Destroy()
	
	print("itemunequipdisconnect")
end
itemToequip = rep:WaitForChild("Events").ItemEquip.OnServerEvent:Connect(itemequip)
itemTodisable = rep:WaitForChild("Events").ItemUnequip.OnServerEvent:Connect(itemunequip)
2 Likes

Nothing, it looks fine to me to be honest.

1 Like

I don’t know how to explain, but you need to :

  1. [Client] Remove all custom function outside an event → equipitem and unequipitem
  2. [Server] Create a local itemClone inside a function and store inside a table, key as playername and value as itemClone
    • because only the last itemClone will be destroy when multiple player clone an item
    • or you can also directly destroy the player tool through player.Character