Setting limit on position tween

Hello there! I am trying to set a limit on my buttons position tween. I have been testing all sorts of methods on how to limit this amount through if statements, yet, I still can’t seem to make it work.

Basically, my tween positions the button properly, but the problem is that if you spam hover your mouse into the radius of the button, the tween starts to go out of place. I will provide a video below of me hovering over the button fast:


And of course, here is the script (a local script):

local button_side_effect = button_object.button_side_effect
local button_text = button_object.button_text

local tween_service = game:GetService("TweenService")
local tween_effect_time = 0.5
local move_amount = 0.05
local original_position = button_object.Position

print(original_position)

button_object.MouseEnter:Connect(function()
	tween_service:Create(button_object, TweenInfo.new(tween_effect_time, Enum.EasingStyle.Quart, Enum.EasingDirection.Out), {BackgroundTransparency = 0}):Play()
	tween_service:Create(button_side_effect, TweenInfo.new(tween_effect_time, Enum.EasingStyle.Quart, Enum.EasingDirection.Out), {BackgroundTransparency = 0}):Play()
	
	if button_object.Position.X.Scale >= button_object.Position.X.Scale + move_amount then
		print("bigger")
	else
		tween_service:Create(button_object, TweenInfo.new(tween_effect_time, Enum.EasingStyle.Quart, Enum.EasingDirection.Out), {Position = UDim2.new(button_object.Position.X.Scale + move_amount, button_object.Position.X.Offset, button_object.Position.Y.Scale, button_object.Position.Y.Offset)}):Play()
	end
end)

button_object.MouseLeave:Connect(function()
	tween_service:Create(button_object, TweenInfo.new(tween_effect_time, Enum.EasingStyle.Quart, Enum.EasingDirection.Out), {BackgroundTransparency = 1}):Play()
	tween_service:Create(button_side_effect, TweenInfo.new(tween_effect_time, Enum.EasingStyle.Quart, Enum.EasingDirection.Out), {BackgroundTransparency = 1}):Play()
	
	if button_object.Position.X.Scale >= button_object.Position.X.Scale - move_amount then
		tween_service:Create(button_object, TweenInfo.new(tween_effect_time, Enum.EasingStyle.Quart, Enum.EasingDirection.Out), {Position = UDim2.new(button_object.Position.X.Scale - move_amount, button_object.Position.X.Offset, button_object.Position.Y.Scale, button_object.Position.Y.Offset)}):Play()
	else
		print("bigger")
	end
end)

Thank you in advance if the issue is solved.

Haven’t read the code, but you can just disconnect any events when the button is clicked.

But once disconnected, wouldn’t you have to reconnect them once the event starts again.

Maybe instead of writing things like the above(a variable) you make them a constant number. making sure that the button always goes back to its old position instead of it being relative to its current position.(Making it go backward)

ex:

button_object.MouseEnter:Connect(function()
	tween_service:Create(button_object, TweenInfo.new(tween_effect_time, Enum.EasingStyle.Quart, Enum.EasingDirection.Out), {BackgroundTransparency = 0}):Play()
	tween_service:Create(button_side_effect, TweenInfo.new(tween_effect_time, Enum.EasingStyle.Quart, Enum.EasingDirection.Out), {BackgroundTransparency = 0}):Play()
	
	if button_object.Position.X.Scale >= button_object.Position.X.Scale + move_amount then
		print("bigger")
	else
		tween_service:Create(button_object, TweenInfo.new(tween_effect_time, Enum.EasingStyle.Quart, Enum.EasingDirection.Out), {Position = UDim2.new(0.4 + move_amount, 5, 18, 0.22)}):Play()
	end
end)

button_object.MouseLeave:Connect(function()
	tween_service:Create(button_object, TweenInfo.new(tween_effect_time, Enum.EasingStyle.Quart, Enum.EasingDirection.Out), {BackgroundTransparency = 1}):Play()
	tween_service:Create(button_side_effect, TweenInfo.new(tween_effect_time, Enum.EasingStyle.Quart, Enum.EasingDirection.Out), {BackgroundTransparency = 1}):Play()
	
	if button_object.Position.X.Scale >= button_object.Position.X.Scale - move_amount then
		tween_service:Create(button_object, TweenInfo.new(tween_effect_time, Enum.EasingStyle.Quart, Enum.EasingDirection.Out), {Position = UDim2.new(0.4 - move_amount, 5, 18, 0.22)}):Play()
	else
		print("bigger")
	end
end)

Well yes, but generally you don’t have UI stay after it’s done being used. You’d just clone it back in or use ResetOnSpawn, which will reload the script anyway (technically a new instance of it, not reloading).

Maybe you just need a debounce variable so if the button position has reached its goal then do nothing or just do what you want next.