How do you cancel a UI:TweenSize()?

About

Hello, I’ve been trying to code a bar that fills up whenever you are holding left click on a certain part at a certain distance.

So far, I’ve gotten everything accomplished except the bar whenever I release the left mouse button it doesn’t cancel the tween.

What I’ve tried

I’ve tried changing the size on button release, and tweening the opposite direction. I’ve sat here for 30 minutes now thinking what I could be doing to do this, what I should be doing, then trying it but it doesn’t come remotely close to what I need (which is for the Bar to reset back to size of 0, 0, 0.7, 0).

My Code

UIS.InputBegan:Connect(function(InputObject, Processed)
	
	if InputObject.UserInputType == Enum.UserInputType.MouseButton1 then
		
		local distance = (char.HumanoidRootPart.Position - mouse.Target.Position).magnitude
		
		mouseDown = true
		
		while mouseDown == true do
			
			wait()
			
			if mouse.Target.Name == "Canvas" and distance < 10 then
				
				bar.Fill:TweenSize(UDim2.new(0.975, 0, 0.7, 0), "Out", "Linear", speed.Value)
				bar.Fill.Size = UDim2.new(0, 0, 0.7, 0)
				
			else
				
				bar.Fill.Size = UDim2.new(0, 0, 0.7, 0)
				
			end
			
		end
		
		if mouseDown == false then
			
			bar.Fill.Size = UDim2.new(0, 0, 0.7, 0)
			
		end
		
	end
	
end)

--\\ Button Release //--

UIS.InputEnded:Connect(function(InputObject, Processed)
	
	if InputObject.UserInputType == Enum.UserInputType.MouseButton1 and mouse.Target.Name == "Canvas" then
		
		mouseDown = false
		
		bar.Fill.Size = UDim2.new(0, 0, 0.7, 0)
		
	end
	
end)

I’d greatly appreciate some help on this, I’ve searched the DevForum multiple times, but haven’t found what I am looking for exactly.

Thank you,

-EnDarke

1 Like

Have you looked into Tween:Cancel()?

image
Source: Tween

Here are the docs for the Cancel function of the tween: TweenBase:Cancel


Note, I think Tween:Cancel only works for tweens created with TweenService, unless you can somehow get the Tween object from :TweenSize(). So to use :Cancel() you would need to have created the tween with TweenService:Create().


1 Like

You can use TweenService scroll down to " Pausing a Tween" As :TweenSize() returns a bool, not a tweenbase.

So in this case, the only way to do this is with TweenService.

You can cancel it though with Tween:Cancel()

:eyes:

1 Like

Pretty sure Tween:Cancel() doesn’t work with UI? I’m not sure, I’ve never done TweenService with UI so if that’s possible I’ll need to check that out.

Correct, said this earlier. TweenService is required to do what @EnDarke is wanting.

1 Like

Thank you two for helping me! I will now be able to get this finished and released soon!

From my experience, TweenService does work with UI.
You would do something like this:

TweenService = game:GetService("TweenService")

-- ...
local FillTween = TweenService:Create(bar.Fill, TweenInfo.new(speed.Value, Enum.EasingStyle.Linear, Enum.EasingDirection.Out), {Size = UDim2.new(0.975, 0, 0.7, 0)})

FillTween:Play()
-- later
FillTween:Pause() -- This stops the tween and keeps the bar filled to the value it is at when the tween is paused
-- or
FillTween:Cancel() -- This immediately stops the tween and resets the bar to be filled as much as it was before you played the tween
4 Likes