Laaaaaaaaaggy script

this lags like hell when i pick up items:

local backpack = game.Players.LocalPlayer:WaitForChild("Backpack")
local inventory = game.Players.LocalPlayer.PlayerGui:WaitForChild("inventory")
local gui = inventory:WaitForChild("maingui")
local items = gui:WaitForChild("items")
local template = game.ReplicatedStorage:WaitForChild("template")
local player = game.Players.LocalPlayer
local function update()
	local setup = items:WaitForChild("setup")
	setup.Parent = gui
	items:ClearAllChildren()
	setup.Parent = items
	local toolCount = 0
	local function countTools(child)	
		local toolCount = 0
		for _, tool in ipairs(backpack:GetChildren()) do
			if tool.Name == child.Name then
				toolCount += 1
			end
		end
		return toolCount
	end

	local owned = {}
	if game.Players.LocalPlayer.Character.Humanoid.Health == 0 then
		owned = {}
	end
	for i,v in pairs(backpack:GetChildren()) do
		local item = v
		local name = item.Name
		local clone = template:Clone()
		local templatename = clone:WaitForChild("name")
		local number = clone:WaitForChild("number")
		number.Text = countTools(item)
		templatename.Text = name
		clone.Parent = items
		if owned[name] then
			clone:Destroy()
		end
		owned[name] = true
	end
end


	
function characterAdded()
	backpack = player:WaitForChild("Backpack")
	backpack.ChildAdded:Connect(update)
end

if player.Character then characterAdded() end
player.CharacterAdded:Connect(characterAdded)

This function is ran for every tool inside the backpack. The function runs a loop again through ever tool in the backpack, which could lead to lag.

1 Like

It is also unnecessary to keep making ChildAdded events everytime the character is added. Then, it will run the update function more times due to the connection still being active.

Store the backpack.ChildAdded:Connect(update) in a variable, then use variable:Disconnect() to disconnect the connection, before making a new one (do this all in your characterAdded function)… Let me know if you’d like me to explain.

sorry, but i vave found a solution because inventory has a keybind


local debounce = false
while wait() do
	local backpack = game.Players.LocalPlayer:WaitForChild("Backpack")
	local inventory = game.Players.LocalPlayer.PlayerGui:WaitForChild("inventory")
	local gui = inventory:WaitForChild("maingui")
	local items = gui:WaitForChild("items")
	local template = game.ReplicatedStorage:WaitForChild("template")
	local player = game.Players.LocalPlayer

	local function update()
		local setup = items:WaitForChild("setup")
		setup.Parent = gui
		items:ClearAllChildren()
		setup.Parent = items
		local function countTools(child)	
			local toolCount = 0
			for _, tool in ipairs(backpack:GetChildren()) do
				if tool.Name == child.Name then
					toolCount += 1
				end
			end
			return toolCount
		end

		local owned = {}
		if game.Players.LocalPlayer.Character.Humanoid.Health == 0 then
			owned = {}
		end
		for i,v in pairs(backpack:GetChildren()) do
			local item = v
			local name = item.Name
			local clone = template:Clone()
			local templatename = clone:WaitForChild("name")
			local number = clone:WaitForChild("number")
			number.Text = countTools(item)
			templatename.Text = name
			clone.Parent = items
			if owned[name] then
				clone:Destroy()
			end
			owned[name] = true
		end
	end

	local UserInputService = game:GetService("UserInputService")
	local key = UserInputService:IsKeyDown(Enum.KeyCode.E)
	if key and not debounce then 
		inventory = script.Parent
		debounce = true
		update()

	end
	if not key then
		debounce = false 
	end
end