Scripting Helpers is winding down operations and is now read-only. More info→
Ad
Log in to vote
0

How can I run a coroutine over and over? [HELP WITH DEAD COROUTINES]

Asked by
C1RAXY 81
2 years ago
Edited 2 years ago

Hi I'm trying to create a simple camera "tween" and after that tween the camera will stay fixed until the dialog ends Video

The issue that I have is that when I first call my coroutine to resume it will run as expected but after calling it again the status will return dead.

here is the code

local CanLoopCam = false

local TweenCam = coroutine.create(function()
    CanLoopCam = false
    if not CanLoopCam then
        for i = 0,0.5,.01 do
            Camara.CFrame = Camara.CFrame:Lerp(PP.Parent.Parent.o.CFrame, i)
            task.wait()
        end
        CanLoopCam = true
        while CanLoopCam do
            task.wait()
            Camara.CFrame = PP.Parent.Parent.o.CFrame
        end
    end
end)

function CANTtalk()
    CanLoopCam = false
    canTalk = true
    Camara.CameraType = Enum.CameraType.Custom
    MainFrame.Dialogo.Text = " "
    humanoid.WalkSpeed = 20
end

function CANtalk()
    print(coroutine.status(TweenCam))
    canTalk = false
    Camara.CameraType = Enum.CameraType.Scriptable
    humanoid.WalkSpeed = 0
    MainFrame.Titulo.Text = npc.Name
    coroutine.resume(TweenCam)
end

I tried yielding but it stops everything. Video

function CANTtalk()
    CanLoopCam = false
    canTalk = true
    Camara.CameraType = Enum.CameraType.Custom
    MainFrame.Dialogo.Text = " "
    humanoid.WalkSpeed = 20
    coroutine.yield(TweenCam)
end

Help is appreciated, I tried a lot of stuff but I'm still learning how to script

0
Have you tried Rerunning it? it seems it only runs once and afterwards it seems it will not rerun because it has already finished. gumpy_e 22 — 2y

1 answer

Log in to vote
1
Answered by
gumpy_e 22
2 years ago

Kind of a hacky solution but it works. when you call CANTtalk() it closes the current TweenCam and when you call CANtalk() it remakes TweenCam and resumes it. Doesnt return dead anymore since its an entirely new TweenCam

local CanLoopCam = false

local TweenCam = coroutine.create(function()
    CanLoopCam = false
    if not CanLoopCam then
        for i = 0,0.5,.01 do
            Camara.CFrame = Camara.CFrame:Lerp(PP.Parent.Parent.o.CFrame, i)
            task.wait()
        end
        CanLoopCam = true
        while CanLoopCam do
            task.wait()
            Camara.CFrame = PP.Parent.Parent.o.CFrame
        end
    end
end)

function CANTtalk()
    coroutine.close(TweenCam)
    CanLoopCam = false
    canTalk = true
    Camara.CameraType = Enum.CameraType.Custom
    MainFrame.Dialogo.Text = " "
    humanoid.WalkSpeed = 20
end

function CANtalk()
    canTalk = false
    Camara.CameraType = Enum.CameraType.Scriptable
    humanoid.WalkSpeed = 0
    MainFrame.Titulo.Text = npc.Name
    coroutine.resume(TweenCam)
    TweenCam = coroutine.create(function()
        CanLoopCam = false
        if not CanLoopCam then
            for i = 0,0.5,.01 do
                Camara.CFrame = Camara.CFrame:Lerp(PP.Parent.Parent.o.CFrame, i)
                task.wait()
            end
            CanLoopCam = true
            while CanLoopCam do
                task.wait()
                Camara.CFrame = PP.Parent.Parent.o.CFrame
            end
        end
    end)
    print(coroutine.status(TweenCam))
end

0
Amazing! Thank you so much for this C1RAXY 81 — 2y
Ad

Answer this question