Function stops rest of code below it instead of continuing?

When I call it, it just never continues below it.
(Part of the code is put into a comment as I was testing it w/o datastores)

Code:

--Handlers
local DataStoreService = game:GetService("DataStoreService")
local DataPlayer = DataStoreService:GetDataStore("PlayerData")
HttpService = game:GetService("HttpService")

--Chat detection
local Admins = {3208036948} --user IDs
local Prefix = "/"  --chat prefix

local Power = script.TronEnabled
local Variant = nil

local coroutineTitantron = nil
local coroutineLEDs = nil

local TronFiles = {
	["Titantron Data"] = {
		["HasIntro"] = true;
		["Wait time"] = 0.5;

		["Intro"] = {
			["TV"] = {1,2,3};
			["TVTop"] = {};
			["Side3"] = {};
			["Side4"] = {};
			["Screen1"] = {};
			["Screen2"] = {}
		};
		
		["Main"] = {
			["TV"] = {4,5,6};
			["TVTop"] = {};
			["Side3"] = {};
			["Side4"] = {};
			["Screen1"] = {};
			["Screen2"] = {}
		}
	};
	
	["LED colors"] = {
		["Color1"] = 0,
		["Color2"] = 0
	}
}

local function RunTV(Data,Type)
	local ImportedData = Data
	local IntroIDs = Data["Titantron Data"].Intro.TV
	local MainIDs = Data["Titantron Data"].Main.TV
	local WaitTime = Data["Titantron Data"]["Wait time"]
	local HasIntro = Data["Titantron Data"].HasIntro
	
	if HasIntro == true and Type ~= "Main" then
		for i,ID in pairs(IntroIDs) do
			if Power.Value == false then
				print("AAAA")
				break
			end
			print(ID)
			wait(WaitTime)
		end
	end
	
	while wait() do
		for i,ID in pairs(MainIDs) do
			if Power.Value == false then
				print("AAAA")
				break
			end
			print(ID)
			wait(WaitTime)
		end
	end

end

--[[script.RemoteEvent.OnServerEvent:Connect(function(player,user,TitantronNumber,option)
	Power.Value = false
	wait(0.5)
	print(Power)
	--fetch user's titantron data depending on the loadout
	local success,Data = pcall(function()
		return DataPlayer:GetAsync(player.UserId)
	end)
	if success then
		local DataImport = Data
		if option == "Intro" then
			Variant = "Intro"
		elseif option == "Main" then
			Variant = "Main"
		end
		--TV
		Power.Value = true
		--coroutine.create(RunTV)
		RunTV(DataImport,Variant)
	end
end)
]]--

Power.Value = true
local connection = RunTV(TronFiles,"Intro")
print("eee")
wait(1)
Power.Value = false

I think it has to do with the actual function call itself, specifically the lack of a returned value.

local connection = RunTV(TronFiles,"Intro")

I would make RunTV return true as soon as it’s finished.

Since your function has some wait()'s, it is yielding. A simple fix would be using a different thread using task.defer like so:

local connection = task.defer(RunTV, TronFiles, "Intro")

OH, about that; I did RunTV() first alone, but that stopped rest of code below it. Then, I added connection = RunTV() to :disconnect() it later on.

This has nothing to do with returns/yielding. I don’t get why it’s not running the rest of the code, this has never happened to me before.

This did the job; I never used task.defer as I mostly relied on coroutines. It’ll come in handy for my other projects! Thanks.

1 Like

coroutines/task.spawn() would have worked fine too, the code just needed to be handled in an alternate thread of execution.