How To Make A Game Loop that Yields for Players?

So I’m trying to make a game that has this basic structure, I’ll make an example:

The game starts, it’s the intermission, and the players are in something like an elevator. The players complete an objective, and must return to the same elevator. Once all are in (or most of them if the timer ends), a new map is loaded, and the cycle continues.

Now this means that a simple while loop won’t be enough, but the problem:

The problem I’m having is actually turning the standard round system that uses while loops into a different structure/method, which I have no clue how to structure. I’m sure it’s pretty simple, but I can’t really wrap my head around it.

I tried checking for values in the while loop structure, but it always ends up as a: repeat task.wait(0.01) until elevatorFilled == true inside the while loop, which is probably not the right move.

Basically, how do I make the game loop so that it waits for the players that make it before the timer ends?

Current Code (There are chapters, split up by levels):

-- RUN TIME --
while true do
	
	--Play Cutscene
	
    --Levels
	for i = 1, 7, 1 do
		Start()
	end
	
	--Next Chapter
	if Maps:FindFirstChild(tostring(ServerInfo.Chapter + 1)) then
		ServerInfo.Chapter += 1
	else
		break
	end
	
	
end

Thanks for helping :happy2:

1 Like

Maybe you should use the loop function.

What type of loop? … max chars…

Oh the while function, maybe try adding one more end when ending the script.

Are you talking about some different form of while loop? Because at first I though doing something like while ElevatorEmpty do but that wouldn’t work.

sry im confused:

maybe something like

local function yield_elevator(max_time : number)
	local time = max_time
	while time > 0 do
		task.wait(1)
		time -= 1
		-- check if all players are in elevator
		if allPlayersAreInElevator() then
			break
		end
	end
end

-- call yield_elevator to wait for all players to get in elevator or max time to be reached.
yield_elevator(30)
print("yield finished")

I’ve done this before, using a state-machine approach where the game progresses through different phases; intermission, gameplay, returning by examples

I’ll give you an example of codes in a few when my studio finishes loading

1 Like
local RunService = game:GetService("RunService")

local function Intermission()
	warn("Intermission started")
	task.wait(10)
end

local function StartLevel()
	warn("Level started")

	local ObjectiveComplete: boolean = false
	local Timer: number = 60 -- Time limit

	-- Wait for objective to be completed / timer to run out
	local StartTime: number = tick()
	while not ObjectiveComplete and tick() - StartTime < Timer do
		task.wait(1)
		-- Check objective completion
		if math.random(1, 10) == 1 then
			ObjectiveComplete = true
		end
	end

	print("Objective complete or timer ended, returning to elevator")
end

local function WaitForPlayersToReturn()
	print("Waiting for players to return...")
	local Timer: number = 10
	local PlayersReturned: number = 0 -- Example count

	local StartTime = tick()
	while PlayersReturned < 5 and tick() - StartTime < Timer do
		task.wait(1)
		
		PlayersReturned = ... -- do ur checks
	end

	print(`{PlayersReturned} players made it before the timer ended`)
end

local function Loop()
	while true do
		Intermission()

		for i = 1, 7 do
			StartLevel()
			WaitForPlayersToReturn()
		end

		warn("Next")
		task.wait(2) -- Delay before next cycle
	end
end

Loop()
2 Likes

I will try to use this and get back to you, thanks

This is great, but is there a way to start the timer after a certain event?

In my game, once alarms go off, or all objectives are completed, a timer will start, that is basically a “Get to the exit before you get caught!”

Thanks

Here is how I would do it in a structured manner using promises:

1 Like

I’ll try to apply it tomorrow, thanks

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