Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
✓ ANSWER ACCEPTED

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

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

1 comment

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.

1 answer

✓ Accepted answer

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

1 comment

0

Amazing! Thank you so much for this

This discussion is preserved as it was. New answers, comments, and votes are closed.