Help with scripting UI system

Hello there! Right now I’m trying to make a simple badge menu as shown below:

Ignore my horrible writing

Now I know the normal way on how to handle this(insert a local script for each text button). What I’m trying to achieve is to have this all on one script, WITHOUT having to create a variable for each text button. How I figure on doing it is this:

TextButtonInfoQuestionAW

As you can see, the grassland text button and the grassland statistics( the image label that is a child of Scores) both have the same name. My idea for creating this system is this:

1.) Detect which text button was clicked and get its name
2.) Use the text buttons name to find the statistic menu that matches it

What I need help with is how to detect when a text button was clicked without creating a variable for each text button.
Here is what I have started so far. I am an intermediate scripter so there is bound to be mistakes:

local scores = script.Parent
local mainMenu = scores.MainMenu
local scrollingFrame = mainMenu.ScrollingFrame
local levels = scrollingFrame:GetChildren()

local function findClicked(course)
	for i, v in pairs(levels) do
		if v:IsA("TextButton") and v.Name == course.Name then
			return course.Name
		end
	end
end 

local function findScoreInfo(info)
	info.Name = findClicked()
	return info.Name
end

--- Idk what to put here

If there is a better way or confused lemme know.

Have a great day!

Why not replace the textbuttons with image buttons?

How is that supposed to help? I am asking how do you detect the mouseClick event without creating variables for every single text button.(they’re textbuttons due to an effect I made when you hover your mouse over the scores)

Heyo, theoretically all you’ll have to do is to iterate through every object in the ScrollingFrame → check if they’re a TextButton → bind the function to the TextButton.MouseButton1Click event.

You can simply do something like this inside the function which is connected to the .MouseButton1Click event:

local statisticMenu = button:FindFirstAncestor("Scores"):FindFirstChild(button.Name)
2 Likes

Can u just clarify what an ancestor is? Is it like a parent?

Correct. :FindFirstAncestor is like :FindFirstChild, but in the opposite direction.

2 Likes

It worked! Thank you so much! Here is the end result:

local scores = script.Parent
local mainMenu = scores.MainMenu
local scrollingFrame = mainMenu.ScrollingFrame

local function getInfo(button)
	local statisticMenu = button:FindFirstAncestor("Scores"):FindFirstChild(button.Name)
	statisticMenu.Visible = true
	mainMenu.Visible = false
end

for i, v in pairs(scrollingFrame:GetChildren()) do
	if v:IsA("TextButton") then
		v.MouseButton1Click:Connect(function()
			local textbutton = getInfo(v)
		end)
	end
end
1 Like