How am I supposed to wait for a tween to complete AND stop when another value is false, correctly?

I can wait for a tween to finish simply by using TweenBase.Completed:Wait(), but what if I wanted to stop this tween (in the middle of it) when another value becomes false?

Place Example: devforum-tween-completed-example.rbxl (34.5 KB)

while true do
	wait()
	if TweenBool.Value == false then
		Frame.Position = UDim2.new(0.338, 0, 0.054, 0)
	else
		local tweenPosInfo
		local tweenPosGoal = {}
			
		if goDown then
			tweenPosInfo = TweenInfo.new(0.6, Enum.EasingStyle.Quart, Enum.EasingDirection.Out)
			tweenPosGoal.Position = UDim2.new(0.335, 0, 0.532, 0)
		else
			tweenPosInfo = TweenInfo.new(0.6, Enum.EasingStyle.Quart, Enum.EasingDirection.In)
			tweenPosGoal.Position = UDim2.new(0.338, 0, 0.054, 0)
		end
			
		local tweenPosition = TweenService:Create(Frame, tweenPosInfo, tweenPosGoal)
			
		goDown = not goDown
		
		tweenPosition.Completed:Wait()
	end
end
1 Like

I’m not an expert on this things, but you could try doing something like:

if sus == true then
tweenPosition:Stop()

The problem is that I want to check during the tween. I’m not sure if there’s something else I can do like repeat wait() until tweenPosition.Completed or TweenBool.Value == false but Completed is not a boolean.

Sorry but i don’t know how to do that.
But maybe you could try making the script check if the part you are moving is in an exact position, and if it is, then it stops the tween.
If that doesn’t work i don’t know what you could do.

To do this you have to use task.spawn() which creates a separate thread where you can check if the value has changed and then stop the tween.

Example:

task.spawn(function()
	repeat
		task.wait()
	until value == false
	
	tween:Stop()
end)

tween.Completed:Wait()

This will now run the function in task.spawn() at the same time tween.Completed:Wait() is running.

5 Likes

Thank you, think works perfectly.

Edit: It’s tween:Cancel() by the way.

1 Like