Too Many Scripts Problem

I’m making an inventory system where each item appears as a small gui in a scrollingframe on the side of the screen. Each has a localscript in it. Each time the inventory updates, it clears all of those gui and creates a clone of a template gui, making it appear as if one was added. This all works fine, however if the script is added, it often floods the output with the error “attempt to index nil with ‘MouseButton1Click’”. This causes a horrid amount of lag. How can I prevent this? Is there a different way I can do this, such as moving it all into one control script? Any helpful answers are appreciated. Please try and keep any answers simple and clear. Thank you :heart:

Could you give some code from the local script you have in each gui?

Try

local totalbuttons= {}

totalbuttons[#totalbuttons+1] = guiobject.MouseButton1Click:Connect(function()
	-- insert code here
end)

How would I define guiobject ?

Here’s the full script.

local Salv = game.Workspace.Salvage
local Board = Salv.InfoBoard.SurfaceGui
script.Parent.MouseButton1Click:Connect(function()
	local Ore = script.Parent.Name
	local RealOre = game.ReplicatedStorage.Ores:FindFirstChild(Ore)
	if PlayerData["Inventory"][Ore] > 0 then
		Board.OreName.Text = Ore
		Board.Depth.Text = "Depth: "..tostring(RealOre.MinDepth.Value).."-"..tostring(RealOre.MaxDepth.Value)
		Board.Rarity.Text = tostring(RealOre.Rarity.Value)
		local Price
		if RealOre:FindFirstChild("Price") then
			Price = tostring(RealOre.Price.Value)
		else
			Price = "Ore cannot be sold."
		end
		if Price then
			Board.Price.Text = Price
		end
		if RealOre:FindFirstChild("RareScript") then
			Board.Ultra.Visible = true
			Board.Rarity.Text = "Rarity: 1/"..tostring(RealOre.RareScript.Value)
		else
			Board.Ultra.Visible = false
			Board.Rarity.Text = "Rarity: "..tostring(RealOre.Rarity.Value)
		end
		local ClonedOre = RealOre:Clone()
		ClonedOre.Parent = Salv
		ClonedOre.CFrame = Salv.Ore.CFrame
		Salv.Ore:Destroy()
		ClonedOre.Name = "Ore"
		Salv.OreSelected.Value = Ore
	end
end)

I believe you can merge all of this into one script. I’m assuming your explorer is something like ScreenGui → Frame → ItemGui → Script. Put a local script with the frame as its parent. Start it off as:

for _, item in (script.Parent:GetChildren()) do
    if item ~= script then
        item.MouseButton1Click:Connect(function()
           --your code
        end)
    end
end

Clicking the GUIs did nothing, the code does not work.

Could you send the explorer of your game?

There’s a lot in workspace. Should I show a snippet of it that has to do with the script? @PengoMilk

Send everything that has to do with the gui of your inventory system.

image