Method for round based game

Hi, I know there are other posts about this topic, but I did not find one that is what I want to do.

In short, I have a round-based game and the minimum players this game can have is 3 players in a round.

So I want it to continually check if there are more than 3 players that want to play, this can easily be done with while-function:

while true do
if playersWhoWantToJoin:GetChildren() > 2 then
–more than 2 players.
end
end

This I know, but what I want to do know is this:

  1. Check if there are more than 2 players, if yes and no game is running, send an invoke to the main script that starts the round (teleports, etc.)
  2. When round gets fininished send an invoke to another script that gives money based on what place you came on.
  3. Wait 30 seconds and start the round again if there are more than 2 players and no game is currently running.

The part that is hard for me is how I can make it so it doesnt send an invoke when a game is running (so it doesnt start multiple rounds etc.) and how the while loop can know that there is a game running + having a pause between each game.

I made a script earlier, where I wrapped everything inside the while loop, but it ended with it beeing very buggy and hard to edit.

I am open to any idea, just how can I make around based game that only starts when more than 2 players are in the game.

I just need an idea, I dont need any code (a little bit would be great) just give me the idea of how I can achieve this.

If you need any more information, just ask.

Thank you for any help! :slight_smile:

Sincerely.

DOES NOT NEED TO USE A WHILE LOOP, THIS WAS JUST MY IDEA. WHATEVER WORKS IS APPRECIATED

So you trying to make a round system and check if the game is not already running? Is that correct?

You can make checks inside the while loops that checks for example when the round starts it does a timer then you check when the timer ends then you check if there are 2 players with #game.Players:GetPlayers() == 2 then you require a module to keep it clean to exectute the start round and you can just do a check for the time with a value that is what I should do if you need further explanation just ask :slight_smile:

1 Like

Could you explain further? Also, it does not need to be a while loop.

Kinda, not my main point.
Basically any system that I can use for a round based game. A system that checks that there are more than 2 players before the round starts.

I should do a round system in one while loop for example like this

while wait(Seconds to wait for following round) do
if #game.Players:GetPlayers() > 2 then
-- Start round here and require a script for example require(script.Module):StartRound()
-- This will run until the round is ended then it will restart the round and check again if there are more than 3 players in the game
else
-- You can also add a message here when there is not enough players
end
-- end Round here
end
2 Likes

You can do something like this:

Round = Path.To.Boolean
CanStart  = true --Set to true after cooldown

local function StartRound() 
if playersWhoWantToJoin:GetChildren() >= 3 then
if (not Round) then CanStart then --To know if there is a round that is currently running
Round = true
CanStart = false
--START ROUND
end
end

Players.PlayerAdded:Connect(StartRound)

Round.Changed:Connect(function(CurrentState)
if CurrentState == false then --To know if round ended
--GIVE REWARD
delay(30, function() CanStart = true StartRound() end)
end
end)

Note that this is just so you get the idea of how to do it.

1 Like

Of course you can modify a lot here to make it work for UI for example

Could you explain a little more? Having some problems understanding the script.
Thanks

@DevLevii I am not sure if having the whole script inside one loop is a good idea. Since that is what I did last time, and it ended pretty buggy

It worked fine for me I used it also in a round based game it works fine as long as you have put it right the game is at the moment offline for maintance right now so sadly I can’t show it. As long you use one loop it works but do not use loops inside the loops since that can go wrong

That’s sadly my problem, so I do not think that will work…

I am not sure how you are making your thing so but I dont use a loop in a loop in my round system so I am sure you can do that without its just like I showed in the example I have given but am not sure what you use for starting the round of course.

Try this, note I wrote that on phone same as this post and I didn’t test that.

local Players = game:GetService("Players")
local Workspace = game:GetService("Workspace")

local GameInProgress = false
local CanStart = true

spawn(function()
	while wait() do
		if #Players >= 3 then
			if CanStart then
				CanStart = false
				wait(20)
				if GameInProgress == false then
					GameInProgress = true
					-- do what ever you need when round starts
					wait(30)
					-- do what ever you need when round ends
					GameInProgress = false
				end
				wait(5)
				CanStart = true
			end
		end
	end
end)

-- Note that I didnt test that because I wrote this on my phone
2 Likes

@Daw588 @AlvinPolys @DevLevii
Thanks for your answers! It really helped me!

2 Likes

No problem, I found issue in my script, instead of local CanStart = false put local CanStart = true. I couldn’t test so that explains everything :slight_smile:

1 Like

Allright! Thanks you for all the help!

1 Like