How to cancel a tween?

Hello,
So I have a this script:

script.Parent.MouseEnter:Connect(function()
	local tweenService = game:GetService("TweenService")
	local length = 0.3
	local tweenInfo = TweenInfo.new(length,Enum.EasingStyle.Linear)
	local info = {}
	script.Parent:TweenSize(UDim2.new(0.564, 0,0.158, 0),Enum.EasingDirection.Out,Enum.EasingStyle.Sine,.1)
	info.Offset = Vector2.new(0.5,0.5)
	tween = tweenService:Create(script.Parent.UIGradient,tweenInfo,info)
	tween:Play()
end)

script.Parent.MouseLeave:Connect(function()
	local tweenService = game:GetService("TweenService")
	local length2 = 0.3
	local tweenInfo2 = TweenInfo.new(length2,Enum.EasingStyle.Linear)
	local info2 = {}
	script.Parent:TweenSize(UDim2.new(0.465, 0,0.141, 0),Enum.EasingDirection.Out,Enum.EasingStyle.Sine,.1)
	info2.Offset = Vector2.new(1,1)
	tween:Cancel()
	tween2 = tweenService:Create(script.Parent.UIGradient,tweenInfo2,info2)
	tween2:Play()
end)

The problem when someone moves their mouse quickly, is that the button gets “stuck”. The tween is still playing when they click off it(which should play the tween to go back). But this doesn’t happen. Is there any way to cancel the tween if it’s playing to prevent this from happening?

1 Like

Tween:Stop() is what you do for it, although im pretty sure there is a bool override parameter to TweenSize, if you set it to true then it cancels the tween happening

Edit: Just saw in the DevHub to confirm and yes there is a bool override parameter, all you gotta do in this is add “, true” in the parameters on TweenSize. Here:

script.Parent:TweenSize(UDim2.new(0.465, 0,0.141, 0),Enum.EasingDirection.Out,Enum.EasingStyle.Sine,.1, true)
2 Likes