Tween resets when mouse leaves GUI

Problem 1:
I’m doing A animation for my buttons when I hover over them with my mouse,
but each time the Tween finishes it will reset the Offset and repeat it, but I want after the tween is finished the offset to stay until I leave the button with my mouse.

Problem 2:
When I leave the button with my mouse while the tween is increasing the offset, and I enter the area with my mouse again, it will reset the time until the required offset is reached, but I want when I enter the button again to just proceed rather than having to wait another 3 seconds.
Example: After 90 seconds (When the Gradient Offset would be 0.5) And I leave & reenter the button it should proceed with the remaining 90 seconds rather than the whole 3 minutes

local TI	=TweenInfo.new(3, Enum.EasingStyle.Circular, Enum.EasingDirection.InOut, 1, false, 0)
for	i,v in pairs(ButtonGradient) do
	local mouseIn
	PlayButton.MouseEnter:Connect(function()
		mouseIn = true
		while mouseIn==true do
			local Rween =TweenService:create(v, TI, {Offset = Vector2.new(1,0)})
			Rween:Play()
			Rween.Completed:Wait()
		end
	end)

	PlayButton.MouseLeave:Connect(function()
		mouseIn = false
		while mouseIn==false do
			local Rween =TweenService:create(v, TI, {Offset = Vector2.new(0,0)})
			Rween:Play()
			Rween.Completed:Wait()
		end
	end)
end```
1 Like

You’re getting this behavior because you have while true do, just remove them to get what you want

local tweenInfo = TweenInfo.new(3, Enum.EasingStyle.Circular, Enum.EasingDirection.InOut, 1, false, 0)

local function mouseEntered(value)
     local tween = TweenService:Create(value, tweenInfo, {Offset = Vector2.new(1,0)})
     tween:Play()
end

local function mouseLeave(value)
    local tween=TweenService:Create(value, tweenInfo, {Offset = Vector2.new(0,0)})
    tween:Play()
end


// I assume you iterate the ButtonGradient whom is a dictionary
// I recommend also adding garbage collector to the RBXConnections
for _, value in pairs (ButtonGradient)
    value.MouseEnter:Connect(mouseEnter)
    value.MouseLeave:Connect(mouseLeave)
end)
1 Like