Is it bad to cycle through functions for a round based system?

I have a system where the round will play through a cycle where the following function is called after the next. I’m unsure if this will cause the game’s memory to clog or lag as the server continues to run. I will provide an example below:

local intermission
local startRound
local endRound

intermission = function()
     if playerInGame < playerShouldBeIngame then
          return
     end
     --Do intermission stuff
     startRound()
end

startRound = function()
     --Do startRound stuff until condition is met
     endRound()
end

endRound = function()
     --Do endRound stuff
     intermission()
end

--when player joins, intermission() will be called

The only time this cycle ends is when the game does not have enough player in the game, but if the game becomes popular and is always being refilled with players, it’s rare for the cycle to break. So I was wondering if this code is fine the way it is without causing to much lag as the server continue to run for long period of time.

1 Like

Looks good, just make sure the function can continue or restart once there’s enough players again. An instance of this is where all the players leave the server, the server doesn’t shutdown, and more players join after the server died.

1 Like
local Phases = {
	Start = function(params)
		-- do stuff
	end,
	Round = function(params)
		-- do stuff

		SomeEvent:Wait()
	end,
	End = function(params)
		-- do stuff
	end,
	Intermission = function(params)
		-- do stuff
	end
}

while Running do
	for phaseName, phaseFunc in pairs(Phases) do
		warn("In phase : " .. phaseName)

		phaseFunc(...)
	end
end
1 Like

I believe that will waste memory, so a loop is probably a better way to go. If I remember correctly, in normal lua, that is under tail calls, which wouldn’t explode the stack, but luau does not have that feature.

2 Likes

Be sure to call :Disconnect() any signal connections which you no longer need. This will probably be the most likely factor to cause any memory issues, assuming the rest of the code doesn’t have any major flaws.

1 Like