What's wrong with this for loop?

I want the GUI buttons change size when the mouse hovers over them. This is achieved with MouseEnter and MouseLeave as shown in the script:

REMOVED

I’ve put print statements for everything, and they all worked, except the ones that should print when Mouse enter/leave functions connect. This means the script doesn’t detect the mouse entering/leaving the button.

I tried this local script with specific buttons, placing the local script as a child inside these buttons and they worked, but when I try to make it work for ALL buttons in my game, it won’t. How can I achieve this?

Any help appreciated, thanks in advance.

2 Likes

You’re using the buttons in StarterGui, not PlayerGui.

local player = game:GetService("Players").LocalPlayer
local playerGui = player:WaitForChild("PlayerGui")

task.wait(5) --give buttons some time to load (only used as example here)

for _, button in next, playerGui:GetDescendants(), nil do
    if v:IsA("TextButton") or v:IsA("ImageButton") then
		local startsize = v.Size
		local hoversize = UDim2.new(v.Size.Width.Scale * 1.05, 0, v.Size.Height.Scale * 2, 0)

		v.MouseEnter:Connect(function()
			v:TweenSize(hoversize,Enum.EasingDirection.Out,Enum.EasingStyle.Sine,.25,true)
		end)

		v.MouseLeave:Connect(function()
			v:TweenSize(startsize,Enum.EasingDirection.Out,Enum.EasingStyle.Sine,.25,true)
		end)
	end
end


TweenSize is deprecated, use TweenService instead (image from documentation)

3 Likes

Fixed it. Thanks for the help.

2 Likes

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.