How would I make a game loop?

I want to make a strategy board game on Roblox called Squared, the issue is, I have no idea on how Im going to make a game loop, and make it possible to check the amount of players to start it and automatically end a match if their is 1 player left.

To check the amount of players :
Use #game.Players:GetPlayers()

To end a match if there is only 1 player left :
Use something like this : if #game.Players:GetPlayers() == 1 then...

2 Likes

This has nothing to do with the title. He’s/She’s trying to detect if their only is one player left standing. In your case you’re trying to find the all the players, not matter if they won. Although I be may wrong.

I concluded from here, I gave him the basic idea of it

Like this?

local Players = game.Players:GetPlayers()

while true do
	if #Players == 4 then
		
	end
end

You probably want to start something once 4 players join like so

local Players = game.Players:GetPlayers()
-- create our event which signals the game has started
local startgame = Instance.new("BindableEvent")

Players.PlayerAdded:Connect(function(player: Player) -- when a player joins run this function
    if #Players == 4 then
         -- we have 4 players! firing this event takes us to the next function at 'startgame.Event'
         startgame:Fire()
    end
end)

startgame.Event:Connect(function()
    -- anytime we call 'startgame:Fire()' we land here, the game has started!
    -- prepare the game board and the players. we could loop after this point too.
    print("Game has started!")
end)

Using bindable events will be very useful, while true is almost always a bad idea and without a wait() call it will break your server by eating up all the CPU time! Using the PlayerAdded Event means the server only checks how many players there are after a new player joins

2 Likes

It shouldn’t be that way, this is because the game needs to automatically end after their is 1 player and the match is still going.

Can you also add comments in the script? its confusing.

I wouldn’t recommend programming your game this way, but you could extend my previous example (now with some comments) to loop until there is one player like this.

startgame.Event:Connect(function()
    while #Players > 1 do
         print("Game code running!")
         task.wait() -- must wait, roblox will kill the server if it's looping without stopping
    end
end)
1 Like

Here’s some pseudocode to get you started. Use a while true loop for the actual game loop.

Create a table called playersInGame
When the game starts
  (start your game process)
For every player in the game, insert their username in the table called playersInGame
When a player dies, remove their username from playersInGame
If the length of playersInGame == 1 then end the game and reward the player
1 Like

My game wont have players dying., their are 50 turns, and each turn, the players can use their cards, or put down factories in a available area.

After the 50 turns, whoever gets the most points that come from their factories, win.