I have a problem with my Backpack (Script)

Replace:

Player.Backpack.ChildAdded:Connect(function(child)
    if not (child:IsA("Tool")) then return end
    if table.find(displayedItems, child) then return end

    table.insert(displayedItems, child)
    createItemFrame(child.Name, item)
end)

With

Player.Backpack.ChildAdded:Connect(function(child)
    if not (child:IsA("Tool")) then return end
    if table.find(displayedItems, child.Name) then return end

    table.insert(displayedItems, child.Name)
    createItemFrame(child.Name, child)
end)

The chicken appears but it has the same bugs as before.
When I press any key it equips me with items with letters.

Same bug as before → as in when you unequip a tool is creates more item frames?

Post your entire script please

local Players = game:GetService("Players")
local ReplicatedStorage = game:GetService("ReplicatedStorage")
local UserInputService = game:GetService("UserInputService")
local Player = Players.LocalPlayer

local RunService = game:GetService("RunService")

function createItemFrame(text, item)
	local cloneItemFrame = ReplicatedStorage.Item:Clone()
	cloneItemFrame.TextLabel.Text = text
	cloneItemFrame.Tool.Value = item.Name
	cloneItemFrame.ImageLabel.Image = item.TextureId
	cloneItemFrame.Parent = Player.PlayerGui.Backpack.Frame;

	cloneItemFrame.ImageButton.MouseButton1Down:Connect(function() 
		local tool = Player.Backpack:FindFirstChild(cloneItemFrame.Tool.Value)
		local character = Player.Character or Player.Character:WaitForChild()
		local humanoid = character.Humanoid;

		local strokeFound = cloneItemFrame:FindFirstChild("UIStroke")

		if strokeFound then
			strokeFound:Destroy();
			humanoid:UnequipTools() 
			return;
		end

		for i, frame in ipairs(Player.PlayerGui.Backpack.Frame:GetChildren()) do
			for i, item in ipairs(frame:GetChildren()) do
				if item.Name == "UIStroke" then
					item:Destroy();
				end
			end
		end

		local stroke = Instance.new("UIStroke")
		stroke.Color = Color3.fromRGB(255, 255, 255);
		stroke.Parent = cloneItemFrame;
		stroke.Thickness = 2.1

		task.wait(0.10)
		humanoid:EquipTool(tool)
	end)
end

-- Create item frames for the tools which are replicated from starter pack
for i, item in ipairs(Player.Backpack:GetChildren()) do
	createItemFrame(tostring(i), item)
end

-- Create item frames every time a NEW item is added to backpack
local displayedItems = {}

Player.Backpack.ChildAdded:Connect(function(child)
	if not (child:IsA("Tool")) then return end
	if table.find(displayedItems, child.Name) then return end

	table.insert(displayedItems, child.Name)
	createItemFrame(child.Name, child)
end)

local KeyCodes = {
	[49] = 1,
	[50] = 2,
	[51] = 3,
	[52] = 4,
	[53] = 5,
	[54] = 6,
	[55] = 7
}

UserInputService.InputBegan:Connect(function(input: InputObject) 
	for i, item in ipairs(Player.PlayerGui.Backpack.Frame:GetChildren()) do
		if item:IsA("Frame") then
			for i, child in ipairs(item:GetChildren()) do
				if child:IsA("TextLabel") then
					if KeyCodes[input.KeyCode.Value] == tonumber(child.Text) then
						local tool = Player.Backpack:FindFirstChild(item.Tool.Value)
						local character = Player.Character or Player.Character:WaitForChild()
						local humanoid = character.Humanoid;

						local strokeFound = item:FindFirstChild("UIStroke")

						if strokeFound then
							strokeFound:Destroy();
							humanoid:UnequipTools() 
							return;
						end

						for i, frame in ipairs(Player.PlayerGui.Backpack.Frame:GetChildren()) do
							for i, item in ipairs(frame:GetChildren()) do
								if item.Name == "UIStroke" then
								item:Destroy();
								end
							end
						end

						local stroke = Instance.new("UIStroke")
						stroke.Color = Color3.fromRGB(255, 255, 255);
						stroke.Parent = item;
						stroke.Thickness = 2.1

						task.wait(0.10)
						humanoid:EquipTool(tool)
					end
				end
			end
		end
	end 
end)

When I press any key it equips me with items with letters

What do you mean?

The chicken appears but it has the same bugs as before.

I assume you mean that whenever you unequip a tool, it creates an extra item frame.
This code:
image
should be preventing that, and I just tested it in studio and it worked fine.

Ill read your script now though

I think I figured it out, remove this bit:

-- Create item frames for the tools which are replicated from starter pack
for i, item in ipairs(Player.Backpack:GetChildren()) do
	createItemFrame(tostring(i), item)
end

It no longer displays the items in Starter Pack.
I made a video to show you the bug.

replace:

-- Create item frames every time a NEW item is added to backpack
local displayedItems = {}

Player.Backpack.ChildAdded:Connect(function(child)
    if not (child:IsA("Tool")) then return end
    if table.find(displayedItems, child) then return end

    table.insert(displayedItems, child)
    createItemFrame(child.Name, item)
end)

with

local displayedItems = {}

-- Create item frames for the tools which are replicated from starter pack
for i, item in ipairs(Player.Backpack:GetChildren()) do
  createItemFrame(tostring(i), item)
  table.insert(displayedItems, child)
end

-- Create item frames every time a NEW item is added to backpack
Player.Backpack.ChildAdded:Connect(function(child)
    if not (child:IsA("Tool")) then return end
    if table.find(displayedItems, child) then return end

    table.insert(displayedItems, child)
    createItemFrame(child.Name, item)
end)

thats my bad, getting hard to track the messages now haha

local displayedItems = {}

-- Create item frames for the tools which are replicated from starter pack
for i, item in ipairs(Player.Backpack:GetChildren()) do
  createItemFrame(tostring(i), item)
  table.insert(displayedItems, item.Name)
end

-- Create item frames every time a NEW item is added to backpack
Player.Backpack.ChildAdded:Connect(function(child)
    if not (child:IsA("Tool")) then return end
    if table.find(displayedItems, child) then return end

    table.insert(displayedItems, child)
    createItemFrame(child.Name, child)
end)

It appears now but there is still the same problem with the items.

local displayedItems = {}

-- Create item frames for the tools which are replicated from starter pack
for i, item in ipairs(Player.Backpack:GetChildren()) do
  createItemFrame(tostring(i), item)
  table.insert(displayedItems, item.Name)
end

-- Create item frames every time a NEW item is added to backpack
Player.Backpack.ChildAdded:Connect(function(child)
    if not (child:IsA("Tool")) then return end
    if table.find(displayedItems, child.Name) then return end

    table.insert(displayedItems, child.Name)
    createItemFrame(child.Name, child)
end)
1 Like

It works but on the other hand the chicken is bugged.
It displays a letter instead of the numbers and when you press any key it equips the chicken.