I have a question about how to best streamline a script in a way that is probably commonly used.
Assume you have a shop Frame with a dozen TextButtons that players might click. You want a script to detect if any of the buttons are clicked and to change the background color of the TextButton. My previous way to script this was straight forward:
Example:
local button1 = script.Parent.Button1
local button2 = script.Parent.Button2
-- Continue through all the buttons
button1.MouseButton1Click:Connect(function()
button1.BackgroundColor3 = Color3.new(0,0,127)
print("Button1 click registered")
end)
button2.MouseButton1Click:Connect(function()
button2.BackgroundColor3 = Color3.new(0,0,127)
print("Button2 click registered")
end)
-- Continue making individual functions for all 12 buttons
This method takes me forever. I was just introduced into using Tables to streamline this process. I’m new at this and could use some suggestions. Here’s my current plan on how to streamline my script to get the same results as above:
local buttonTable = {
button1 = script.Parent.Button1,
button2 = script.Parent.Button2,
-- Continue through all 12 buttons
}
for i, v in pairs(buttonTable)
v.MouseButton1Click:Connect(function()
v.BackgroundColor3 = Color3.new(0,0,127)
print(v.Name .. " click registered")
end)
end
Will this work well? Will the script monitor for all the button clicks when written like this? Does a wait need to be added? Other considerations?
Yes, but you can make it better! You’re on the right track, and that’s a wonderful improvement, happy your learning better techniques - but try putting all the buttons in one frame/folder or something, then looping through it like so:
for _,v in pairs(buttonFrame:GetChildren()) do
v.MouseButton1Click:Connect(function()
v.BackgroundColor3 = Color3.new(0,0,127)
print(v.Name .. " click registered")
end)
end
This way, you don’t need to manually specify each button, it’ll automatically find them for you. Makes your code cleaner and easier to maintain. Add a new button, and it’s automatically connected without touching the code at all.
I usually create all of them in runtime and then inside the frame creation function I have handling for the mouse click. If it is not made in runtime you could simply loop through all the children of the frame you are using and see if it is a frame (or image label), like @Xeptix suggested. Good luck!
Nice! Learning how to use Tables decreased the amount of typing I did by half. You showing the simplicity of placing all the buttons in a folder and using :GetChildren just decreased my typing in half again!!! Part of me wants to go back to my existing scripts and streamline them similarly, but nah. I’ll do it from now forward. Thanks!
Your suggestion sounds worth looking into. I’m thus far unfamilar with using runtime. Can you point me in a direction to learn more about runtime in a Roblox context?
Oh, maybe I should have explained better. What I mean by “making frames in runtime” is having a module script that holds all the “shop info” and then looping through that in a local script to make all of the frames provided with the information on each item. Here is info on module scripts: ModuleScript | Documentation - Roblox Creator Hub