For i, v in pairs() text button bug

I’m trying to make a system that equip a tool when it’s on the player storage, but this weird bug has appeared.
BTW: the buttons are clones

My code:

for i, child in pairs(Client.PlayerGui:WaitForChild('StorageUI'):WaitForChild('Background'):WaitForChild('List'):GetDescendants()) do
	if child:IsA('TextButton') then -- It print's fine
		child.MouseButton1Click:Connect(function() --// This is the problem
			print('a')
			if ReplicatedStorage.Items:FindFirstChild(child.Name) then
				print('hi')
				local newitem = ReplicatedStorage.Items:FindFirstChild(child.Name):Clone()
				newitem.Parent = Client.Backpack
				child:Destroy()
			else
				print('Not Found: '..child.Name)
			end
		end)
	end
end

What does your output look like when you click a button?

It print’s nothing, I don’t know why

This looks like a Server Script, in which case you need to move your code to a LocalScript and have the buttons tell the Server when they are clicked, since MouseButton1Click is not replicated to the Server.

It’s already on a LocalScript, and not on the Server.

Get rid of space after the comma.

thats not anywhere helping to solve the issue

@OP

did you verify that this part runs?

if child:IsA('TextButton') then

so that the innards actually do get connected as a listening function

Hmmmmm . . . . . . . . . . . .

it does, i have already tested it, i tried printing it.

Are the textbuttons inserted with a for i,v loop as well or are they set up before this loop runs?

Nope, they are not, The only different for i,v loop that is in the script it’s to create the buttons.

Does that loop run before or after the one with the function?

after, but it only is triggered later

If you run the loop with the function before the buttons are created the function isn’t going to work. Make sure you first insert the buttons and THEN run the loop with the function.
Do it like this:
-Insert the buttons
-Run the for i,v loop with the function

2 Likes

That worked! Thank you so much!

1 Like