How should I return a function from a connection?

As the title suggests, I need to return the main function from the first connection in that function.
This might be simple and I’m just stupid sooo here’s the script

local function actualGame()
	script.forceEndMatch.Changed:Connect(function()
		if script.forceEndMatch.Value == true then
			if killer == nil then
				GuiLine.Value = "Killer left!"
			else
				GuiLine.Value = "Match force ended."
				killer.CameraMode = Enum.CameraMode.Classic
			end
			if script.forceEndMatch.Value == true then
				for i, v in pairs(playerTable) do
					v:LoadCharacter()
				end
			end
		end
	end)	

	script.CompletedObjective.Changed:Connect(function()
		if script.CompletedObjective.Value == true then
			script.CompletedObjective.Value = false
			local checkTable = {}
			for i, v in pairs(currentObjectives) do
				if v.completed.Value == true then
					table.insert(checkTable, v)
				end
			end
			local result = CheckTableEquality(currentObjectives, checkTable)
			if result == true then
				GuiLine.Value = "All objectives complete."
				coroutine.wrap(function()
					wait(6.5)
					GuiLine.Value = ""
				end)()	
			end
		end
	end)
end

Someone has suggested I use an event and :Wait(), but that wouldn’t work as I have more things to run below (plus I might need to add more as this is unfinished)

Could you be a bit more descriptive? What is the main function you’re referring to? What exactly are you trying to accomplish?

I’m guessing you’re trying to get the function to yield until the first connection has completed am I correct?

If so, it’s a bit messy with the way you have it setup, but you can always yield the thread with a coroutine, and then resume after running checks in that first connection. It’s not the prettiest, but it’ll get the job done.

local function actualGame()
    local thread = coroutine.running()
	script.forceEndMatch.Changed:Connect(function()
		-- Doing first connection checks
        -- If checks pass, resume the coroutine
        coroutine.resume(thread)
	end)	

	script.CompletedObjective.Changed:Connect(function()
		-- Doing second connection checks
	end)

    coroutine.yield()
end

I might’ve read it wrong however as it wasn’t really worded the best, so lmk if this isn’t what you’re looking for.

Sorry if that didn’t exactly explain.
Basically, in the script.forceEndMatch.Changed:Connect(function() I want to be able to return / end actualGame(), because as the name implies I want to force end the match. Putting a return in the connection only returns that connection.

Edit: BTW the script.CompletedObjective.Changed:Connect(function() is just there so you know there’s more in the script, that part works fine. I’m only trying to do what I mentioned above.

Hope this explains it

someone please solve this i have the same issue :sob:

Returning from a function inside an event connection doesn’t directly affect the enclosing function. However, you can manage the control flow by using flags and checking these flags at appropriate points in your code.

Here’s an example:

local function actualGame()
    local running = true
    
    local function endGame()
        running = false
        if killer == nil then
            GuiLine.Value = "Killer left!"
        else
            GuiLine.Value = "Match force ended."
            killer.CameraMode = Enum.CameraMode.Classic
        end
        for _, player in pairs(playerTable) do
            player:LoadCharacter()
        end
    end

    script.forceEndMatch.Changed:Connect(function()
        if script.forceEndMatch.Value == true then
            endGame()
        end
    end)

    script.CompletedObjective.Changed:Connect(function()
        if running and script.CompletedObjective.Value == true then
            script.CompletedObjective.Value = false
            local checkTable = {}
            for _, objective in pairs(currentObjectives) do
                if objective.completed.Value == true then
                    table.insert(checkTable, objective)
                end
            end
            local result = CheckTableEquality(currentObjectives, checkTable)
            if result == true then
                GuiLine.Value = "All objectives complete."
                coroutine.wrap(function()
                    wait(6.5)
                    if running then
                        GuiLine.Value = ""
                    end
                end)()
            end
        end
    end)

    while running do
        wait(1)
        -- Regularly check the running flag to determine if the game should continue
    end
end

actualGame()

The key here would be the running flag.