I wanted to answer your question even though you already marked a solution, loops will only be expensive if your doing costly items in them, usually involving some great deal of mathematical calculation, and if they are ran excessively. For something like UI elements looping over them would not really affect performance so much, if done correctly.
I just wanted to add that how you were originally doing it is technically slightly faster than they way @RetributionVI suggested, you can read about it here on devforum https://devforum.roblox.com/t/the-art-of-micro-optimizing/226753 , in this post the persons clearly states that:
“Using ipairs
on anything but LuaJIT is just dumb. It’s super, super slow. The previously mentioned numeric loop does everything it does faster (as well as in LuaJIT, but the difference is significantly reduced). If you care at all about performance, please do not use it.”
A simple google search will also confirm his argument, and also i think ipairs is the slowest way to go through a table and you would be better off using pairs.
If your wondering what LuaJIT is a faster version of Lua, but the numeric loop you had before is actually better performance-wise, although this difference isn’t very significant I wanted you to get the best answer, so the best way to do it would to be your way combined with @RetributionVI like this:
for i = 1, #ButtonFolder do
ButtonFolder[i].InputBegan:Connect(function(inputObject)
if inputObject.UserInputType == Enum.UserInputType.MouseButton1 then
print("clicked")
elseif inputObject.UserInputType == Enum.UserInputType.MouseMovement then
print("mouse enter")
end
end)
Button[i].InputEnded:Connect(function(inputObject)
if inputObject.UserInputType == Enum.UserInputType.MouseMovement then
print("mouse left")
end
end)
end
--[[
Also thought it would be worth mentioning the IsA()
function, this can be used on any roblox instance, such
as parts, ui objects, and so on. For example to check
if an item is a Button u could do this:
if Button:IsA("TextButton") or Button:IsA("ImageButton") then
end
--just letting u know about that function
]]
here is a link to the IsA API:Instance | Documentation - Roblox Creator Hub
Just wanted to add a tip, try not to optimize to a point where your code becomes non-readable and non-maintainable in order to gain a few milliseconds of performance