Dynamically setting GUI text button text

Hi, title explain what im trying to do pretty much. I’m trying to change the name of text buttons on dynamically and i’m not quite sure how to go about doing that. Here is code that I commented in so It can help explain what im trying to do better, just incase you need it.

local localBuildableframes = game.Players.LocalPlayer.PlayerGui.Backpack.BackpackUI.Frame:GetChildren() -- List of frames
	for i,v in pairs(localBuildableframes) do -- for each frame
		if v.Name == "TextButton" then -- check if its a text button
			for i,o in pairs(itemCategories['Buildables']) do -- this is where im confused, dont think i should have a for loop in a foor loop lol.
				print(o) -- this prints out the data twice, for the sake of space, its printing out (x,y,x,y)
				v.Text = o  -- trying to set the text of the frame, to o, which would be x, and y. (not x,y,x,y)
				--[[v.MouseButton2Down:Connect(function()
					print('r u being clicked')
					v:Destroy()
					RemoveItem:InvokeServer(o)
				end)]]--
			end
		end
	end

This is the end result. RobloxStudioBeta_wZlZrTMhRD

(should say buildables_table1 and 2, not 1 twice. this is happening because of the for loop above)

I understand why this isnt working, but im not sure how to go about properly doing it.

1 Like

In this case yes. But they can be useful in other places, Just not here.
Basically what you are doing is looping through every frame and then for every frame you are looping through the buildables and setting the name so its gonna give you ‘Buildables_Table1’ each time.
What you want to do is loop through the buildables first and get the index of the frame you want to change.
Example:

for index, name in pairs(buildables) do
    local frame = localBuidableFrames[index] // Gets the frame at position index so first frame, second frame, etc
    if not frame:IsA("TextButton") then continue end
    frame.Text = name
   -- then do your mouse stuff here
end
1 Like

This makes sense, and I actually didn’t know about then continue learning two things at once.
however, it’s still bugged.

RobloxStudioBeta_CAfaJkUglS

Now its only changing the name of one of the textboxes

Thats weird I just tested it and it worked fine for me
‘Buildables_Table1’ and 2 exist right?
Mine was a little more dumbed down but it worked fine.

An error that happened to me when using for loops was when I used the same index variable (i) for a loop inside of a loop. As I can see, you use the “i” variable in both loops, which could possibly throw it off. Perhaps try changing one “i” variable to something else.

RobloxStudioBeta_NsSpsyCCZw

yep, both exist, tried a few different things and its still only doing it to one. aaaaaa

Maybe try printing the index, buildable name and the name of the textbutton?
Idk why it isn’t working but from that screenshot I’m sure that your game is setup a lot differently.

RobloxStudioBeta_31qvIZZiIj

The worst part is it knows both tables are there as well, im stumped. I’ll give a crack at it later.