Detect which ui descendant has been clicked and do action based off descendants name

I’m coding a UI for the player to select an item to wear when they join and save it to a data store, however I’m not sure if there’s an efficient way to detect which descendant of the screen ui has been clicked?

Is there any way to do this rather than writing a separate click detection function for each button descendant in the screen ui?

The current code I’ve got so far looks like this:
image
I will have to code so many buttons individually if I continue like this.

1 Like

Make a for loop that loops trough all the descendants. In that for loop add a Connect function just like you did. in the function you can type SkinTone = options[For Loop Index].Name.
Kinda like this:
for i = 1, #options:GetChildren() do
options[i].MouseButton1Click:Connect(function()
SkinTone = options:GetChildren()[i].Name
end)
end

1 Like

is there any possible way to do this without a loop, or make it so that it only checks for children which are buttons?

1 Like

Using a loop would be the best way. to check if the curretn child is a button you can change the script to be like this:

local children = options:GetChildren()
for i = 1, #children do
if children[i]:IsA(“TextButton”) then
options[i].MouseButton1Click:Connect(function()
SkinTone = children[i].Name
end)
end
end

2 Likes

Ill test this out right now, thanks!

did it work? and sorry if it seems like i rushed my messages. I kinda did do that since i didnt have much time on me

1 Like