Problems with my simple custom inventory

local Player = game.Players.LocalPlayer
if Player.Character or Player.Character.CharacterAdded then
	Character = Player.Character
end
local Current = 1

game:GetService('StarterGui'):SetCoreGuiEnabled(Enum.CoreGuiType.Backpack, false)

CurrentSpots = {
	[1] = false;
	[2] = false;
	[3] = false;
	[4] = false;
	[5] = false;
}


for i, v in pairs(script.Parent:GetChildren()) do
	if v:IsA("ImageButton") then
		v.IMG.MouseButton1Click:Connect(function()
			print(v)
			Current = v
			Character.Humanoid:UnequipTools()
			for ind, ve in ipairs(CurrentSpots) do
				if tostring(ind) == tostring(Current) then
					Character.Humanoid:EquipTool(Player.Backpack[tostring(ve)])
				end
			end
		end)
	end
end

function Add(Tool)
	for i, v in ipairs(CurrentSpots) do
		if v == false then
			CurrentSpots[i] = Tool
			Current = i
			script.Parent[tostring(i)].IMG.Image = Tool.TextureId
			break
		end
	end
end


function Remove(Tool)
	for i, v in ipairs(CurrentSpots) do
		print(v, Tool, Current,i)
		if tostring(v) == tostring(Tool) and tostring(Current) == tostring(i) then
			CurrentSpots[i] = false
			script.Parent[tostring(i)].IMG.Image = ""
		end
	end
end

Character.ChildRemoved:Connect(function(Child)
	if Child:IsA("Tool") and Child.Parent ~= Player.Backpack then
		Remove(Child)
	end
end)

Character.ChildAdded:Connect(function(Child)
	if Child:IsA("Tool") then
		New = true
		for i, v in ipairs(CurrentSpots) do
			if v == Child then
				New = false
				break
			end
		end
	end
	if New == true then
		Add(Child)
	end
end)

Hello, I’ve been trying to fix this problem for around 2 hours now and cannot find any solution;
If you drop an item, then equip another item, it clones the item you dropped back into the GUI, while you do not have the item inside your backpack. So the script kind of fools itself into thinking that you still have the item, while you actually do not. It also gives this error as it tries to equip it:

https://gyazo.com/821867ee88a2747011d932e23d8397b6

Why is this happening? I’ve tried to make the item delete itself it can’t find the item inside the backpack, but no luck.

If you wanna recreate it, heres how it looks like in the explorer:
https://gyazo.com/2d7fbad492cc870c05f8f8b1d88353a1

Heres a GIF of it happening:

https://gyazo.com/bb5f4031260364e73b324b0efe4c92f3

1 Like

Could you provide a repro file for this please?

Your issue is that you’re adding the tool to the inventory every time the character equips a tool; you aren’t checking to see if the character already has that tool.

2 Likes