Detect when tween animation starts

Ive been trying to detect whenever a tween starts for alot of time, but i cant. Ive looked at many videos but they were only about detecting when it ended. My goal is to detect when the tween starts, but i dont know how. Thank you!

3 Likes

You can do a coroutine if necessary, but I dont completely understand what you need

ex.

local tween = TS:Create(...)

local onStart = coroutine.wrap(function()
   --here what you want to do when started
   print("tween started")
end)

tween:Play()
onStart() --REMEMBER TO PLACE THIS EVERYTIME YOU START THE TWEEN

1 Like

Hello there,

If you want something to happen on the go, then you can just write your logic below as soon as your Tween starts.

Or nerduke1’s approach if you want to continue with some other logic while also running the logic you want to run aside as soon as the tween starts.

-- After your Tween starts, you can write your logic below the `Play` function:
tween:Play()
-- your logic, as soon as the tween starts
-- limitation: Doesn't work for concurrent execution, concurrent means something that occurs at the same time

Or use:

tween:Play()
task.spawn(function() -- We are spawning a function that runs separately!
    -- Your logic here, runs concurrently with the tween
end)

-- -- You can run other code here

As this allows for asynchronous execution. You can run other code while the tween is animating. Useful for complex scenarios where you need to do things in parallel with the animation. Coroutines are more useful as well, but at this point, task.spawn might as well suffices just as well depending on your needs, task.spawn works just as well as coroutine does in simpler scenarios.

Coroutines offer more fine-grained control for intricate logic over execution compared to task.spawn, but they can be slightly more complex to understand.

Both coroutines and task.spawn achieve concurrency, the differences lies in that just coroutines offer more control over execution flow, but for simpler scenarios, task.spawn also suffices.

You can use tween.PlaybackState to detect if the tween has begun

    tween.PlaybackStateChanged:Connect(function(playbackState)
        if playbackState == Enum.PlaybackState.Playing then
            print("Tween started for", instance)
        end
    end)
1 Like

Additional detail, the tween will loop forever.

1 Like

Like is it a moving part? or something else

Basically. It will change the size. Here is the tween code:

local part = game.Workspace.errorbox
local tweenservice = game:GetService("TweenService")

local tweeninfo = TweenInfo.new(
	0.5, --Number of seconds the tween will get to its target
	Enum.EasingStyle.Quad, --Easingstyle
	Enum.EasingDirection.Out, --Easingdirection
	-1, --How many times it will loop (-1 is forever)
	false, --Will it reverse back after done playing
	1 --Wait before starting
)

local properties = {
	["Size"] = Vector3.new(5, 5, 5) --I define this as the target
}

local tween = tweenservice:Create(part, tweeninfo, properties) --Creating the actual tween
tween:Play() --Play the tween
local part = game.Workspace.errorbox
local tweenService = game:GetService("TweenService")

local tweenInfo = TweenInfo.new(
    0.5, -- Number of seconds the tween will take to reach its target
    Enum.EasingStyle.Quad, -- Easing style
    Enum.EasingDirection.Out, -- Easing direction
    -1, -- How many times it will loop (-1 is forever)
    false, -- Should it reverse after reaching the target?
    1 -- Delay before starting
)

local properties = {
    ["Size"] = Vector3.new(5, 5, 5)
}

local tween = tweenService:Create(part, tweenInfo, properties)

-- Detect when the tween starts
tween.PlaybackState:Connect(function(state)
    if state == Enum.PlaybackState.Playing then
        print("Tween started!")
    elseif state == Enum.PlaybackState.Completed then
        print("Tween completed!")
    elseif state == Enum.PlaybackState.Cancelled then
        print("Tween cancelled!")
    end
end)

-- Start the tween
tween:Play()

tell me if it works, i don’t exactly know what you’re trying to do.

1 Like

A simple method could be to use a loop as shown below:

while tast.wait(0.5) do
--Code here
end

This will loop forever with a delay of 0.5 seconds between each time.

1 Like

Im trying to make a pulsating like box that plays a sound whenever it pulses, or in other words whenever the tween starts. I ran your script but nothing really happened. Thanks anyways!

did it print? thats what was supposed to happen

It did not print anything. (thirteui charactererrs)

I had to edit it to this instead for it to work:

game.Workspace.RemoteEvent.OnServerEvent:Connect(function(player)
	tween:Play()
	while task.wait(1) do
		game.Workspace["elevator ding"]:Play()
		task.wait(0.5)

Thank you for the suggestion!

1 Like

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