Function only executing the first part

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!

Add a unique print after the loops and a if statements, this will help pinpoint the issue
Example

for i,v in pairs(workspace:GetChildren()) do
	print(v.Name)
	if v.IsA("Part") then
		print(`{v.Name} is a {v.ClassName}`)
	end
end
print("loop successful")

This makes no sense for the issue?

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

There are already prints, it only prints the first print on the output as I said in the post.

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”.
image

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

Hello, I tried this and it didn’t work. It’s returning me an error at the (values:GetChildren()) part.

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)

oh sorry just take away the :GetChildren()

1 Like

It’s working now, thank you so much!

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.