How do I stop this from happening to my UI?

So I made a script that multiplies the button size by 1.1 when you hover over it. BUT, when I continuously hover between multiple ones it kinda shrinks the button. How do I fix this? Here’s my script and demonstration on what happens.

for i,button in pairs(script.Parent:GetDescendants()) do
	if button:IsA("TextButton") or button:IsA("ImageButton") then
		button.MouseEnter:Connect(function()
			script.Parent["ui hover"]:Play()
			button:TweenSize(UDim2.new((button.Size.X.Scale * 1.1), (button.Size.X.Offset), (button.Size.Y.Scale * 1.1), (button.Size.Y.Offset)), Enum.EasingDirection.Out, Enum.EasingStyle.Sine, 0.2, true)
		end)
		button.MouseLeave:Connect(function()
			script.Parent["ui hover"]:Play()
			button:TweenSize(UDim2.new((button.Size.X.Scale / 1.1), (button.Size.X.Offset), (button.Size.Y.Scale / 1.1), (button.Size.Y.Offset)), Enum.EasingDirection.Out, Enum.EasingStyle.Sine, 0.2, true)
		end)
	end
end
1 Like

You could probably add a debounce

local debounce = false
for i,button in pairs(script.Parent:GetDescendants()) do
	if button:IsA("TextButton") or button:IsA("ImageButton") then
		button.MouseEnter:Connect(function()
			if debounce then return end
			debounce = true
			script.Parent["ui hover"]:Play()
			button:TweenSize(UDim2.new((button.Size.X.Scale * 1.1), (button.Size.X.Offset), (button.Size.Y.Scale * 1.1), (button.Size.Y.Offset)), Enum.EasingDirection.Out, Enum.EasingStyle.Sine, 0.2, true)
		end)
		button.MouseLeave:Connect(function()
			script.Parent["ui hover"]:Play()
			button:TweenSize(UDim2.new((button.Size.X.Scale / 1.1), (button.Size.X.Offset), (button.Size.Y.Scale / 1.1), (button.Size.Y.Offset)), Enum.EasingDirection.Out, Enum.EasingStyle.Sine, 0.2, true)
			task.wait(.1)
			debounce = false
		end)
	end
end```

nope, still happens, even with the debounce

1 Like

You are multiplying the current scale by 0.1. You should actually save the original size and use it. Sometimes you may hover while its half tweened and your final size would be 1.5 and not exact.

for i,button in pairs(script.Parent:GetDescendants()) do 
	if button:IsA("TextButton") or button:IsA("ImageButton") then
        local originalSize = button.Size
		button.MouseEnter:Connect(function()
			script.Parent["ui hover"]:Play()
			button:TweenSize(UDim2.new((originalSize.X.Scale * 1.1), (originalSize.X.Offset), (originalSize.Y.Scale * 1.1), originalSize.Y.Offset)), Enum.EasingDirection.Out, Enum.EasingStyle.Sine, 0.2, true)
		end)
		button.MouseLeave:Connect(function()
			script.Parent["ui hover"]:Play()
			button:TweenSize(UDim2.new((originalSize.X.Scale / 1.1), (originalSize.X.Offset), (originalSize.Y.Scale / 1.1), (originalSize.Y.Offset)), Enum.EasingDirection.Out, Enum.EasingStyle.Sine, 0.2, true)
		end)
	end
end
3 Likes

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