I’m trying to make a button that get all values inside of a folder parented to the LocalPlayer and then looks for the tools that have the same name in a different folder.
My problem is that the function is only executing the first part, because of the first message being the only printing in the Console.
local folder = Instance.new("Folder")
folder.Name = "KioskItems"
folder.Parent = game.Players.LocalPlayer
end)
local valuesFolder = game.Players.LocalPlayer:WaitForChild("KioskItems")
local toolsFolder = game:GetService("ReplicatedStorage").Tools
script.Parent.MouseButton1Click:Connect(function()
task.spawn(function()
local values = {}
for _, item in ipairs(valuesFolder:GetChildren()) do
-- do something with each item
table.insert(values, item.Value)
print("Value added to table")
end
local tools = {}
for _, item in ipairs(toolsFolder:GetChildren()) do
if item.Name == values[1] then -- assume first value is tool name
local tool = item:Clone()
table.insert(tools, tool)
print("Tool added to table")
end
end
local player = game.Players.LocalPlayer
for _, tool in ipairs(tools) do
tool.Parent = player.Backpack
print("Tried to give tool to player")
end
end)
end)
This script is a LocalScript inside of a SurfaceGui in StarterGui.
Any kind of help is appreciated!
You said the issue was that the full function wasnt running, so, to pin point where exactly the issue is (where its stopping), you need to add more prints, the statements in my prints where just an example, they can be anything
script.Parent.MouseButton1Click:Connect(function()
task.spawn(function()
local values = {}
for _, item in ipairs(valuesFolder:GetChildren()) do
-- do something with each item
table.insert(values, item.Value)
print("Value added to table")
end
print("first loop ran")
local tools = {}
for _, item in ipairs(toolsFolder:GetChildren()) do
if item.Name == values[1] then -- assume first value is tool name
local tool = item:Clone()
table.insert(tools, tool)
print("Tool added to table")
end
end
print("second loop ran")
local player = game.Players.LocalPlayer
for _, tool in ipairs(tools) do
tool.Parent = player.Backpack
print("Tried to give tool to player")
end
end)
print("third loop ran")
end)
what dark means is to add a print after each loop to see if they even get a chance to run
All loops have run now, although the tool isn’t in my inventory. It only shows the first print which is “Value added to table” in the console and then the “loop ran”.
script.Parent.MouseButton1Click:Connect(function()
task.spawn(function()
local values = {}
for _, item in ipairs(valuesFolder:GetChildren()) do
-- do something with each item
table.insert(values, item.Value)
print("Value added to table")
end
local tools = {}
for _, item in ipairs(toolsFolder:GetChildren()) do
for _, v in pairs(values:GetChildren()) do
if item.Name == v then
local tool = item:Clone()
table.insert(tools, tool)
print("Tool added to table")
end
end
end
local player = game.Players.LocalPlayer
for _, tool in ipairs(tools) do
tool.Parent = player.Backpack
print("Tried to give tool to player")
end
end)
end)
try this
You assumed that the first value is the tool name but you should check all the values
script.Parent.MouseButton1Click:Connect(function()
task.spawn(function()
local values = {}
for _, item in ipairs(valuesFolder:GetChildren()) do
-- do something with each item
table.insert(values, item.Value)
print("Value added to table")
end
local tools = {}
for _, item in ipairs(toolsFolder:GetChildren()) do
for _, v in pairs(values) do
if item.Name == v then
local tool = item:Clone()
table.insert(tools, tool)
print("Tool added to table")
end
end
end
local player = game.Players.LocalPlayer
for _, tool in ipairs(tools) do
tool.Parent = player.Backpack
print("Tried to give tool to player")
end
end)
end)