CloseCoroutine while playing function

Hello developers, i need help trying to end a function that is being ran by a coroutine,

local twe = game:GetService("TweenService")
local PreStationNodes = game.Workspace.Coasters.Track.MovementNodes.PreStationNodes
local function GetTime(part1, part2)
	local distance = (part1.Position - part2.Position).Magnitude
	local speed = 10
	return distance / speed
end

local function TweenCar(Model)
	for i=1,20 do
		local Time = GetTime(Model.PrimaryPart, PreStationNodes[i])
		local movement = twe:Create(Model.PrimaryPart, TweenInfo.new(Time, Enum.EasingStyle.Linear), {CFrame = PreStationNodes[i].CFrame})
		movement:Play()
		movement.Completed:Wait()
		if i == 20 and Model.Name == "Front" then
			-- End Coroutine here.

		end
	end
end

local TweenFront = coroutine.create(TweenCar)
local TweenCar1 = coroutine.create(TweenCar)
local TweenCar2 = coroutine.create(TweenCar)
local TweenCar3 = coroutine.create(TweenCar)

coroutine.resume(TweenFront,script.Parent.Front)
coroutine.resume(TweenCar1,script.Parent["1"])
coroutine.resume(TweenCar2,script.Parent["2"])
coroutine.resume(TweenCar3,script.Parent["3"])

Now I’ve tried to put this in the line where I want to close it

coroutine.close(TweenFront)
coroutine.close(TweenCar1)
coroutine.close(TweenCar2)
coroutine.close(TweenCar3)

But the script doesn’t know what its closing, how can I make this work?

Define the variables outside the scope of the functions (ie. at the top of the script). But closing it wont work as it’ll say ‘cannot close running coroutine’. return nil to end the function and put the coroutine in a dead state instead

That seems to work the way i want it to, thanks!

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