How to check if a tween is finished?

I remember seeing some function or whatever its called, that waits for a tween to finish playing. Unless I’m delusional, but I can’t seem to find it anywhere in the documentation.

--[services]--

local TweenService = game:GetService("TweenService")

--[variables]--

local grad = script.Parent.UIGradient
local gradRotateTi = TweenInfo.new(2, 
	Enum.EasingStyle.Linear,
	Enum.EasingDirection.In,
	0)

local gradRotate1 = TweenService:Create(grad, gradRotateTi, {Rotation = 180})
local gradRotate2 = TweenService:Create(grad, gradRotateTi, {Rotation = 0})


--[functions]--

function gradLoop()
	while true do
		grad.Rotation = 0
		gradRotate1:Play()
		 if grad.Rotation == 180 then
		    grad.Rotation = -180
			gradRotate2:Play()
		end
		task.wait(gradRotate2) -- How to wait for tween to finish tweening?
end

--[Start of file]--

if script.Parent.Visible == true then 
		gradLoop()
		print("Useless script is successfully running or you're blind.") -- but it isn't..so..
	end
end

I know I could probably use an if statement to check the rotation value or simply wait for the tween time of 2, but it just seems unproductive and I want to learn more efficient functions that I can use in the future.

Currently the print statement doesn’t even output so I am quite sure this is the issue here…

Tween.Completed:Wait()

1 Like
1 Like

Hold up, I’ve implemented the changes

function gradLoop()
	while true do
		grad.Rotation = 0
		gradRotate1:Play()
		if gradRotate1.Completed:wait() then
			gradRotate2:Play()
		end
		gradRotate2.Completed:wait()
end

But it still seems that the tween isn’t working…any idea what might be the issue? Or is this not the way to use it.

You don’t need to add the if statements, its basically just waiting for the tween to finish

2 Likes

I noticed 2 issues, W isn’t capitalized when doing gradRotate1.Completed:Wait() and I noticed that the function “gradLoop” doesn’t end. maybe this could work:

function gradLoop()
	while true do
		grad.Rotation = 0
		gradRotate1:Play()
		gradRotate1.Completed:Wait()
		gradRotate2:Play()
		gradRotate2.Completed:Wait()
	end
end
1 Like

yep I overlooked…thanks for pointing it out. Its working now, just a little too fast.

Just another question: Is there a better way to rotate the entire gradient like 360 instead of using 2 separate tweens?

1 Like

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.