Scripting Helpers is winding down operations and is now read-only. More info→
Ad
Log in to vote
0

Script help? (Summarizing an event)

Asked by 7 years ago

There's gotta be a better way to shorten this. I'm trying to make a function where it detects when one of its child's ImageButtons have been clicked, and then identifies the button that was clicked. It is way easier than to make a new function for every single button. How should I do this? Is there a certain function or event to do so?

Thanks, hopefully I explained this correctly.

1 answer

Log in to vote
0
Answered by 7 years ago

yes, there is.

you'll want to use a for loop and :GetChildren() to identify all the image buttons.

local frameWithImageButtons = script.Parent --where are all the image buttons located?
local imageButtons = frameWithImageButtons:GetChildren()
for i=1, #imageButtons do --loop through all the buttons
     --Connect to the buttons click event
    local imageButton = imageButtons[i] --get the image button in the array
    imageButton.MouseButton1Click:Connect(function()
        --[[imageButton is a local variable unique in the scope of one iteration of the for loop,
        so it will connect to each individual button.]]
        print(imageButton.Name..' was clicked.')
    end)
end
1
Why not use a Generic-For instead of Numeric in this case? OldPalHappy 1477 — 7y
0
numeric is slightly faster Disillusions 61 — 7y
0
Wow. The difference is so small it's not even worth it. Use generic loops. Link150 1355 — 7y
0
it literally doesn't matter Disillusions 61 — 7y
Ad

Answer this question