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:
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.)
When round gets fininished send an invoke to another script that gives money based on what place you came on.
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!
Sincerely.
DOES NOT NEED TO USE A WHILE LOOP, THIS WAS JUST MY IDEA. WHATEVER WORKS IS APPRECIATED
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
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
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.
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
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