How to detect if tween is currently playing?

So I’ve been trying to solve this issue for a long time, the function below is connected to a Touched Event, and it will fire every time a player touches a part.

My problem is that this function gets fired way too many times when a player touches a part. I know the reasonable way would be to add a debounce for the touched event but that does not help for my specific case.

What I’m trying to achieve is, I want to be able to know inside the function,
if the tweens are currently playing, if they are playing, do nothing, if they are not playing, proceed with the function and play the partTween and ReverseTween.

I tried looking into TweenBase.PlayBackState but I have no idea how to format it with this.
Any help would be appreciated!


local function createTween(part, platestringvalue, trapstringvalue)
	

	local primary = trapstringvalue.Parent.primary

	local info = TweenInfo.new(
		--time, easingstyle repeatcount(negative number loops indefinitely), reverses(true/false), delay
		0.3,
		Enum.EasingStyle.Linear,
		Enum.EasingDirection.Out,
		0,
		false
	)

	local primaryposition = primary.CFrame

	local position = {
		CFrame = primary.CFrame + Vector3.new(0,6,0)
	}

	local position2 = {
		CFrame = primaryposition
	}

	local partTween = TweenService:Create(primary, info, position)
	
	local function ReverseTween(Tween, ReverseProperty, DelayTime)
		Tween.Completed:Connect(function()
			local tweenproperty = ReverseProperty
			wait(DelayTime) ---or add this to the actual tween info
			local ReversedTween = TweenService:Create(primary, info, tweenproperty)
			ReversedTween:Play()
			return
		end)
	end
	
		
	partTween:Play()
	ReverseTween(partTween, position2,  1)
	
	
end

I dont think. but you can do something like

local myTweenCompleted = false

yourTween.Completed:Connect(function() myTweenCompleted = true end)
3 Likes

The Tween class has a property called PlaybackState. This value is a PlaybackState enum which can be any of these options. So, if you wanted to check if a tween had completed, you’d do something like this:

if tween.PlaybackState == Enum.PlaybackState.Completed then
	print("Tween completed!")
end
28 Likes

I have found that .Completed is for me personally hard to use and sometimes inconsistent. If you need to have the script wait for the tween to finish, I prefer to use simple wait(#) rather than using .Completed.

Ty, that actually helped a bit! :smiley: