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.
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?
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
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
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)