How can I stop my tween?

Hello everyone!

So I’ve been trying to make that Camera animation on the background, and I’m stuck here.
When I hit “play”, the Camera doesn’t reset back to the player’s head.

Tried searching, nothing worked.

Here’s my code:

-- Defining Stuff
local camera = game.Workspace.CurrentCamera
local TS = game:GetService("TweenService")
local TI = TweenInfo.new(25)
local TC = { CFrame = CFrame.new(Vector3.new(-150.3, 24.099, 29.589)) * CFrame.Angles(0, math.rad(90), 0) }
TW = TS:Create(camera, TI, TC)
local stopPlaying = false

-- Changing initial Camera Type
camera.CameraType = Enum.CameraType.Scriptable

-- Repeating Background Animation

repeat
    print(script.stopPlaying.Value)
    camera.CFrame = CFrame.new(Vector3.new(86.1, 24.099, 29.589)) * CFrame.Angles(0, math.rad(90), 0)
    TW:Play()
    wait(25)
until script.stopPlaying.Value == true

-- Function to Stop Playing.

function onCalled()
    script.stopPlaying.Value = true
    print(stopPlaying)
    local character = game.Players.LocalPlayer.Character
    camera.CameraSubject = character.Humanoid
    camera.CameraType = Enum.CameraType.Custom
    camera.CFrame = character.Head.CFrame
    TW:Cancel()
    local TW2 = TS:Create(camera, TweenInfo.new(0.1), {CFrame = character.Head.CFrame})
    TW2:Play()
end
7 Likes

Looks like Tween’s have :Pause() and :Cancel()

So TW:Pause() should stop it. TW:Cancel() returns everything to what it was before the Tween

24 Likes

Already tried that (you can see that in my code)

As @CompilerError mention, tweenservices have a built in :Pause() and :Cancel() function built in, it should also be classified you MUST have this have the tween itself defined preferrably by a local function in the script.

2 Likes

Sorry, don’t use Cancel. Use Pause, cancel will undo the Tween.

2 Likes

You have not locally defined TW, TW2 and TS

1 Like

Right now it’s getting a nil variable.

1 Like

Hi. It is defined on the script, just forgot to copy it. Editing my code.

Can you open up console via roblox studio and provide a screenshot at all of any error codes that are to do with this script?

1 Like

I feel really dumb. As I was reading my code, I noticed that my function that stopped the repeat loop was… after the repeat loop, so it didn’t run. Thanks for helping anyways.

3 Likes

Thank you for using devforum help section, if you need any further help with programming and/or any developer related questions, please do not hesitate to open another post and our developers will help you to the best of the best of their abilities,

Have a good day @Mehdi_ea :smile: