player = game.Players.LocalPlayer
playerinWorkspace = player.Character.Name
inventory = script.Parent.Parent
inv_shown = false
local itemtoReplace

function replace()
	local item = script.Parent.theItem.Value
	if item then
		if item.itemEquipped.Value == true then
			print("This is already equipped")
		else
			if item.itemType.Value == "Weapon" then
				for j,k in pairs(player.Equip:GetChildren()) do
					if k.itemType.Value == "Weapon" then
						local itemName = k.Name
						k:Destroy()
					end
				end
				wait()
				itemtoReplace.itemEquipped.Value = false
				local itemtoEquip = item:Clone()
				itemtoEquip.Parent = player.Equip
			end
		end
	end
end

function itemEquip()
	local item = script.Parent.theItem.Value
	if item then
			if item:findFirstChild("itemLevel") then
				if player.Stats.Lvl.Value < item.itemLevel.Value then
					print("Put Errror Here")
				end
			end
			local found_wep = false
			if item:findFirstChild("itemType") then
				if item.itemType.Value == "Weapon" then
					for j,k in pairs(player.StarterGear:GetChildren()) do
						if k.className == "Tool" then
							found_wep = true
						end
					end
					for j,k in pairs(player.Equip:GetChildren()) do
						if k.className == "Tool" then
							found_wep = true
						end
					end
					for j,k in pairs(player.Character:GetChildren()) do
						if k.className == "Tool" then
							found_wep = true
						end
					end
					if found_wep == false then
						item.itemEquipped.Value = true
						local clon = item:Clone()
						clon.Parent = player.Equip
						script.Parent.Visible = false
					else
						item.itemEquipped.Value = true
						itemtoReplace = item
						print("Replace? Message")
						replace()
					
					end
				elseif item.itemType.Value == "Helm" or item.itemType.Value == "Chest" or item.itemType.Value == "Back" then
					item.Parent = player.Equip
					for j,k in pairs(player.Equip:GetChildren()) do
						if k ~= item then
							if k:findFirstChild("itemType") then
								if k.itemType.Value == item.itemType.Value then
									k.Parent = player.Inventory
								end
							end
						end
					end
				end
			end
	end
end

script.Parent.Equip.MouseButton1Click:connect(itemEquip)

I'm trying it so that the equip button will not work when an item is equipped, but will work on other items which will make the previous item (the equipped one) be unequipped and the new item equipped. However, it gets stuck on the second item with both items having the true value for their itemEquippeds