My script doesn't work

local Menu = script.Parent.Parent
local MenuFrame = Menu:WaitForChild("MenuFrame")

local ToolFrame = MenuFrame:WaitForChild("ToolMenu")
local Parent = script.Parent


local tab = {}

function addItem(what)
	local Sample = ToolFrame.Example:Clone();
	table.insert(tab, Sample);
	Sample.Info.Text = what;
	Sample.Parent = ToolFrame
end;

game.Players.PlayerAdded:Connect(function(LocalPlayer)
     for i,v in pairs(LocalPlayer.Backpack) do
		 wait(5)
         addItem(v)
     end
end)

Can anyone help with this script? It’s not doing anything/working.

1 Like

You cannot iterate over an Instance, to get all the children, instead use Backpack:GetChildren()

In that same line, you index LocalPlayer which does not exist. Instead use game.Players.LocalPlayer

All in all, it should look like this:

for i,v in pairs(game.Players.LocalPlayer.Backpack:GetChildren()) do

Edit: Forgot ‘Backpack’, thanks FestivePara!

2 Likes
for i, v in pairs(game.Players.LocalPlayer.Backpack:GetChildren()

:wink: (you forgot the Backpack part)

2 Likes

Look at the 4th line in this post. You are trying to set text as an instance instead of string. Replace that line with Sample.Info.Text = what.Name. Also here:

for i,v in pairs(LocalPlayer.Backpack) do
		 wait(5)
         addItem(v)
     end

you need to place a table as an argument to the ipairs() instead of instance. And you want to say you aren’t getting any errors? Or you just don’t know how to open output?

1 Like

You where using an incorrect way to reference the Player inside a LocalScript. For local scripts the player can be referenced with game.Players.LocalPlayer, what happened here is that the script was waiting for another player to be added after the local one was added. I tried cleaning your code and fixing the issue:

--define the services you use
local Players = game:GetService("Players")

--more reliable way to get the local player
--the other one was probably causing your issue
local Player = Players.LocalPlayer

local Menu = script.Parent.Parent
local MenuFrame = Menu:WaitForChild("MenuFrame")

local ToolFrame = MenuFrame:WaitForChild("ToolMenu")
local Parent = script.Parent

local tab = {}

--please use proper name for your variables!
function addItem(item)
	local Sample = ToolFrame.Example:Clone()
	table.insert(tab, Sample)
	--item.Name is a string value describing the object name
	Sample.Info.Text = item.Name
	Sample.Parent = ToolFrame
end

--:GetChildren() returns a table containing all the object children(in this case, tools)
for i, tool in pairs(Player.Backpack:GetChildren()) do
	--use the task library(task.wait), wait() is deprecated
	task.wait(5)
	addItem(tool)
end
1 Like

If it’s not giving any errors, where is this script placed? Is it a Script or a LocalScript?

I’m assuming the script is inside a GUI due to these couple lines. If the function of the script is to create UI elements of tools in the local player’s backpack, it may be better to replace the PlayerAdded connection with a line that defines the LocalPlayer.

This is how I would configure the code to work:

local LocalPlayer = game:GetService("Players").LocalPlayer

local Menu = script.Parent.Parent
local MenuFrame = Menu:WaitForChild("MenuFrame")

local ToolFrame = MenuFrame:WaitForChild("ToolMenu")
local Parent = script.Parent


local tab = {}

function addItem(what)
	local Sample = ToolFrame.Example:Clone();
	table.insert(tab, Sample);
	Sample.Info.Text = what.Name;
	Sample.Parent = ToolFrame
end;

for i, v in ipairs(LocalPlayer.Backpack:GetChildren()) do --Loop through existing tools
	task.wait(5)
	addItem(v)
end

LocalPlayer.Backpack.ChildAdded:Connect(function(item) --Wait for new tools
	task.wait(5)
	addItem(item)
end)

Ah I already fixed my issue, thanks though, but I have another problem.


-- ToolFrame is a frame btw.
for _, Button in pairs(ToolFrame:GetDescendants()) do
    if Button:IsA('TextButton') then
        Button.MouseEnter:Connect(function()
            Button.BackgroundColor3 = Color3.fromRGB(213, 213, 213)
        end)

        Button.MouseEnter:Connect(function()
            Button.BackgroundColor3 = Color3.fromRGB(255, 255, 255)
        end)
    end
end

This isn’t even doing anything at all.

You are using the same event for both color changes, instead you should replace one of them with MouseLeave, also you aren’t checking for new tools being added.

--made it different function for easier use.
function Colors(button)
	--if button isn't an actual button, ignore it
	if not button:IsA("TextButton") then return end 
	
	button.MouseEnter:Connect(function()
		button.BackgroundColor3 = Color3.fromRGB(213, 213, 213)
	end)

	--my guess is that MouseLeave should be here
	button.MouseLeave:Connect(function()
		button.BackgroundColor3 = Color3.fromRGB(255, 255, 255)
	end)
end

for _, Button in pairs(ToolFrame:GetDescendants()) do
	Colors(Button)
end
ToolFrame.DescendantAdded:Connect(Colors) 
1 Like