how would i get my game to start when there are a specific amount of players in the game. I have a status bar which is called SideText. What would be the best way to add a function which sees if there are more than 2 players, if not then the SideText will say waiting for players.
here is my main script:
local Countdown = game:GetService('ReplicatedStorage'):WaitForChild('Values'):WaitForChild('Countdown')
local SideText = game:GetService('ReplicatedStorage'):WaitForChild('Values'):WaitForChild('SideText')
local MapchooseName = game:GetService('ReplicatedStorage'):WaitForChild('Values'):WaitForChild('MapchooseName')
local DefaultSpawn = game:GetService('Workspace'):WaitForChild('Main'):WaitForChild('Lobby'):WaitForChild('Spawnpoints')
local cooldown = 1
while game:GetService("RunService").Heartbeat:Wait() do
for _,plr in next, game.Players:GetPlayers() do
for i = 20,0,-1 do
wait(1)
Countdown.Value = i
SideText.Value = ' Seconds until the next match'
if i == 0 then
wait()
print('game is starting')
wait(cooldown)
SideText.Value = ' The game is now starting'
local mapchoose = math.random(1,2)
MapchooseName.Value = mapchoose
game:GetService('ServerStorage'):WaitForChild(MapchooseName.Value).Parent = game:GetService('Workspace')
local ToolToEquip = game:GetService('ReplicatedStorage'):WaitForChild('Tools'):WaitForChild(1) or game:GetService('ReplicatedStorage'):WaitForChild('Values'):WaitForChild('EquippedTool')
local ToolToEquipCloned = ToolToEquip:Clone()
ToolToEquipCloned.Parent = plr.Backpack
local SpawnToTeleport = math.random(1,5)
local Character = plr.Character
Character:MoveTo(game:GetService('Workspace'):WaitForChild(mapchoose):WaitForChild('Spawns'):WaitForChild(SpawnToTeleport).Position + Vector3.new(0))
for i = 30,0,-1 do
wait(1)
SideText.Value = ' Seconds until match ends'
Countdown.Value = i
if i == 0 then
ToolToEquipCloned:Destroy()
wait()
local DefaultSpawnLocation = math.random(1,5)
Character:MoveTo(game:GetService('Workspace'):WaitForChild('Main'):WaitForChild('Lobby'):WaitForChild('Spawnpoints'):WaitForChild(DefaultSpawnLocation).Position + Vector3.new(0))
Countdown.Value = 0
wait(0.5)
SideText.Value = 'Round has ended'
wait(1)
MapchooseName.Value = mapchoose
game:GetService('Workspace'):WaitForChild(MapchooseName.Value).Parent = game:GetService('ServerStorage')
end
end
end
end
end
end
SideText.Value = ' Seconds until the next match'
Hey I noticed a few things about your post you may want to take a look at so me and others can help. Firstly there is a typo in the title you may want to correct and you should format any code so its easily read. print("hi") < Just like this
To format code you’ll want to use the ` on each side.
First you shouldn’t make a while function. You could just use the event playerAdded. With a variables of the current amount of player, you increment it when a player join and check if the value >= 2. Also check playerRemoving to remove 1 from the variables.
Also when giving your script, you could use ```
And your code here and end with 3`
MainGame() is ment to call the function that runs your main game, I strongly advise you dont just copy paste scripts without understanding how they work
As previously stated I strongly recommend you learn how the script you are running works before using it, else you will find it hard for people to help you.
local players = game:GetService("Players")
local plrInServ = 0 --That will be the count of plr
players.PlayerAdded:Connect(function(plr) --That will create an event listener and fire when a new player join the server
plrInServ = plrInServ + 1
print("Server have now : "..plrInServ.." players")
if plrInServ >= 2 then
--game can start because there is more than 2 players in serv
print("We can play")
end
end)
players.PlayerRemoving:Connect(function(plr) --That will create an event listener and fire when a player leave the server
plrInServ = plrInServ - 1
print("Server have now : "..plrInServ.." players")
end)
Hope it will solve your problem!
Also if it solved your problem, dont forgot to mark as reply the solution.
local playersRequired = 5
game.Players.PlayerAdded:Connect(function(player)
player.CharacterAdded:Connnect(function(character) --[[ only start the game once
the character has loaded.]]
if(#game.Players:GetPlayers() > playersRequired - 1) then --[[ Check if the number is
greater than the playersRequired variable - 1, which means a number equal to the orignal
variable or greater.
]]
-- start the game here!
end
end)
end)
This may be useful with while loops like @anon92559147 uses. You can use the spawn function to create a new thread for your code to run meaning while loops won’t block the code after them.
spawn(function()
while wait(1) do
print("I waited a second!")
end
end)
while wait(2) do
print("I waited two seconds!")
end
I somewhat agree with you and it depends on your specific game but what about when a player leaves? Or when a new round starts? If the game ends prematurely? A lot of games will most likely use another “main loop” somewhere so I usually merge everything into one location/loop so I can modify it easier.