Is there a better (more optimised and efficient) way to handle mouse hovering when you’re dealing with lots of buttons? I could continously add functions to detect hovering for every button but that would go on for lines and lines when there is only 1 line inside the function itself.
-- ResultFunctions
function MouseEnter(Button)
HoverSound:Play()
Button.BackgroundColor3 = Color3.fromRGB(40, 40, 40)
end
function MouseLeave(Button)
HoverSound:Play()
Button.BackgroundColor3 = Color3.fromRGB(35, 35, 35)
end
-- TriggerFunctions
Ethics.Button.MouseEnter:Connect(function()
MouseEnter(Ethics.Button)
end)
Ethics.Button.MouseLeave:Connect(function()
MouseLeave(Ethics.Button)
end)
then on and on and on for every button...
The easiest way to do this IF all the buttons aren’t under one parent is to tag all of them and then loop through the descendants of your screen GUI and check their tags to find your buttons. After finding them, you do the connection stuff.
I would recommend implementing a loop, which iterates through every button and processes it accordingly.
For example, the following code goes through every button within script.Parent and processes it.
for _, Button in script.Parent:GetDescendants() do
if Button:IsA("UIButton") then
Button.MouseEnter:Connect(function()
MouseEnter(Button)
end)
Button.MouseLeave:Connect(function()
MouseLeave(Button)
end)
end
end