Inventory Manager creates multiple client tools

I’m currently experimenting with module scripts and have been going on with them for a bit. I have one certain function that doesn’t seem to be working as expected.

Currently, I’ve tried making the script more “messy” and figuring out if the checkers are off and so on but I haven’t found any sign of that.

Let me explain the functions that currently work so it doesn’t look weird in the scripts:

module:AddItem() ~ Adds an item to the inventory folder
module:RemoveItem() ~ Removes an item from the inventory folder (1 amount, not fully)
module:DeleteItem() ~ Completely removes item from inventory

module:EquipItem() is the one im having issues with.
Basically it goes smooth till you actually pull out the weapon, where it then creates client copies of the tool and just makes it all go crazy. Keep in mind that I only want 1 tool in the character/backpack at the same time.

In the video down below, you can see how only 1 tool exists in the server as it should but multiple are created and multiplied.

2021 11 26 09 37 31 - YouTube

The following scripts are associated with equipping tools etc. !!Note that all of the other functions do not have anything to do with cloning so it couldn’t be any of them!!

The following script is from the server core:

-- Inventory Configurations

game.ReplicatedStorage.Events.InventoryEvent.OnServerEvent:Connect(function(plr, toolName)
	local inventory = plr:WaitForChild("Inventory")
	if inventory:FindFirstChild(toolName) then
		
		local amount = #Tools:GetDescendants()
		for i, v in pairs(Tools:GetDescendants()) do
			if v.Name == toolName then
				InventoryManager:EquipItem(plr, v, inventory:FindFirstChild(toolName).Value)
			else
				amount = amount -1
				if amount == 0 then
					game.ReplicatedStorage.Events.Thought:FireClient(plr, "Couldn't find " .. toolName .. " anywhere in the tools. Report to a developer.")
				end
			end
		end
		
	end
end)

The following script is the ENTIRE client script:

-- Inventory Configurations

local InventoryFolder = plr:WaitForChild("Inventory")



function UpdateInventory()
	for i, v in pairs(InventoryFolder:GetChildren()) do
		for _, currentFrameOBJ in pairs(coreui.Inventory.ScrollingFrame:GetChildren()) do
			if not coreui.Inventory.ScrollingFrame:FindFirstChild(v.Name) then
				local InventoryFrameButton = script.Templates.Inventory.ItemFrame:Clone()
				InventoryFrameButton.Name = v.Name
				InventoryFrameButton.Image = "rbxassetid://" .. tostring(GameDataManager.Images[v.Name]) or "rbxassetid://19679227"
				--InventoryFrameButton.TextLabel.Text = v.Name
				InventoryFrameButton.Parent = coreui.Inventory.ScrollingFrame
			end
		end
	end
end


local function ToggleBag()
	coreui.Inventory.Visible = not coreui.Inventory.Visible
	soundfolder.Slide:Play()
	UpdateInventory()
end

InventoryFolder.ChildAdded:Connect(function()
	UpdateInventory()
end)

InventoryFolder.ChildRemoved:Connect(function()
	for _, currentFrameOBJ in pairs(coreui.Inventory.ScrollingFrame:GetChildren()) do
		if not InventoryFolder:FindFirstChild(currentFrameOBJ.Name) then
			if currentFrameOBJ:IsA("ImageButton") then
				currentFrameOBJ:Destroy()
			end
		end
	end
end)



UserInputService.InputBegan:Connect(function(InputObject, Processed)
	if InputObject.KeyCode == Enum.KeyCode.G then
		ToggleBag()
	end
end)


ContextActionService:BindAction("Inventory", ToggleBag, true, Enum.KeyCode.G, Enum.KeyCode.ButtonR1)
ContextActionService:SetPosition("Inventory", UDim2.new(1, -70, 0, 10))
ContextActionService:SetImage("Inventory", "rbxassetid://311226700")

The following script is put into the frames which activates the remote. It was not my intention to make it run with scripts all over but I didn’t have the patience

 -- This script is inside of the ItemButtons themselves.
script.Parent.MouseButton1Click:Connect(function()
	if game.Players.LocalPlayer.PlayerGui.CoreFunctionality.Templates.Inventory.CanUse.Value == true then
		game.ReplicatedStorage.Events.InventoryEvent:FireServer(script.Parent.Name)
		print("Sent request on " .. script.Parent.Name)		
	end
end)

If you still want to know how the inventory works;
When you add an item, an int value is made and put into the folder. If there is already an int value, the value of the int value will go up by 1. The values of everything in there is 1 or over as they are the amount of that item they have. Ex. Lavender.
Removing it basically removes 1 from that amount and delete completely wipes it.

function UpdateInventory()
     for i, v in pairs(InventoryFolder:GetChildren()) do
	     if not coreui.Inventory.ScrollingFrame:FindFirstChild(v.Name) then
                local InventoryFrameButton = script.Templates.Inventory.ItemFrame:Clone()
				InventoryFrameButton.Name = v.Name
				InventoryFrameButton.Image = "rbxassetid://" .. tostring(GameDataManager.Images[v.Name]) or "rbxassetid://19679227"
				--InventoryFrameButton.TextLabel.Text = v.Name
				InventoryFrameButton.Parent = coreui.Inventory.ScrollingFrame
		end
	end
end

try replacing updateinventory with this, srry bout the indents, made this here

Upped the script itself but didn’t fix the problem :sob:

can u show the bug in action w/ video?

Here it is, you can also find it in the post itself.

In the video, I’m constantly switching from 1 & 2 while re-selecting.

I found the bug, it was the hotbar itself that was messing with it. This has been fixed. Thank you for contributing and optimizing.

1 Like

Can you share the definition of EquipItem from the ModuleScript?