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 simplewhileloop 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
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
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()