Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
✓ ANSWER ACCEPTED

Easier method to find all buttons instead of individually defining them (GetChildren for buttons?)

So I wanted to use GetChildren to just define all the buttons under this GUI and basicly just print the Name of the button you clicked so like,

buttons = script.Parent:GetChildren() buttons.MouseButton1Click:Connect(function() print(buttons.Name) end)

But naturally this didn't work so I tried a different method and I don't know what the heck happend but this is all more complicated then it should be, can anyone recommend a better method?

local children = script.Parent:GetChildren()
local buttons = {}
for i, child in ipairs(children) do
	if child.ClassName == "TextButton" then
		print(child.Name .. " is child number " .. i)
		table.insert(buttons, child.Name)
	end
end
i = 0
i = i + 1
a = i
script.Parent:FindFirstChild(buttons[i]).MouseButton1Click:Connect(function()
	print(buttons[a])
end)
i = i + 1
b = i
script.Parent:FindFirstChild(buttons[i]).MouseButton1Click:Connect(function()
	print(buttons[b])
end)
i = i + 1
c = i
script.Parent:FindFirstChild(buttons[i]).MouseButton1Click:Connect(function()
	print(buttons[c])
end)
i = i + 1
d = i
script.Parent:FindFirstChild(buttons[i]).MouseButton1Click:Connect(function()
	print(buttons[d])
end)
i = i + 1
e = i
script.Parent:FindFirstChild(buttons[i]).MouseButton1Click:Connect(function()
	print(buttons[e])
end)
i = i + 1
f = i
script.Parent:FindFirstChild(buttons[i]).MouseButton1Click:Connect(function()
	print(buttons[f])
end)
i = i + 1
g = i
script.Parent:FindFirstChild(buttons[i]).MouseButton1Click:Connect(function()
	print(buttons[g])
end)
i = i + 1
h = i
script.Parent:FindFirstChild(buttons[i]).MouseButton1Click:Connect(function()
	print(buttons[h])
end)
i = i + 1
j = i
script.Parent:FindFirstChild(buttons[i]).MouseButton1Click:Connect(function()
	print(buttons[j])
end)
i = i + 1
k = i
script.Parent:FindFirstChild(buttons[i]).MouseButton1Click:Connect(function()
	print(buttons[k])
end)
i = i + 1
l = i
script.Parent:FindFirstChild(buttons[i]).MouseButton1Click:Connect(function()
	print(buttons[l])
end)
i = i + 1
m = i
script.Parent:FindFirstChild(buttons[i]).MouseButton1Click:Connect(function()
	print(buttons[m])
end)
i = i + 1
n = i
script.Parent:FindFirstChild(buttons[i]).MouseButton1Click:Connect(function()
	print(buttons[n])
end)
i = i + 1
o = i
script.Parent:FindFirstChild(buttons[i]).MouseButton1Click:Connect(function()
	print(buttons[o])
end)
i = i + 1
p = i
script.Parent:FindFirstChild(buttons[i]).MouseButton1Click:Connect(function()
	print(buttons[p])
end)

1 answer

✓ Accepted answer

Loop through all button

local buttonholder = script.Parent.Holder

for i,v in pairs(buttonholder:GetChildren()) do
	v.MouseButton1Click:Connect(function()

	end)
end

This discussion is preserved as it was. New answers, comments, and votes are closed.