Run code only when 2 players are in-game

Hi, I want to make my script happen only if there are a certain amount of people in the game. (2)
I would like some help if any of you can do that for me.

Here`s my script:
Also I have a value that defines the text on a label I have if you are wondering.

local s = script.Stat
local vals = game.ReplicatedStorage.vals
t = 0

while true do
	t = 20
	repeat
		t = t-1
		s.Value = "Intermission... "..t
		wait(1)
	until t == 0
	s.Value = "Game starting!"
	wait(2)
	local mapselect = game.ReplicatedStorage.Games:GetChildren()
	local choose = math.random(1,#mapselect)
	curnum = 0
	for i =1,#mapselect do
		curnum = curnum +1
		if curnum == choose then
			mapselect[i]:Clone().Parent = workspace
		curmap = mapselect[i].Name
			s.Value = "Map: "..mapselect[i].Name
		end
	end
	wait(3)
	local plrs = game.Players:GetChildren()
	for i = 1,#plrs do
		local num = math.random(1,32)
		plrs[i].Character.Head.CFrame = CFrame.new(workspace.Teleports["Part"..num].Position)
		plrs[i].Character.Parent = workspace.Ingame
	end
	t=100
	repeat
		t = t-1
		s.Value = t.." seconds left"
		wait(1)
	until t ==0 or vals.Winner.Value ~= ""
	if vals.Winner.Value ~= "" then
		s.Value = vals.Winner.Value.. " has won!"
		game.Players[vals.Winner.Value].leaderstats.Points.Value =game.Players[vals.Winner.Value].leaderstats.Points.Value +50
		game.Players[vals.Winner.Value].leaderstats.Wins.Value = 	game.Players[vals.Winner.Value].leaderstats.Wins.Value +1
		vals.Winner.Value = ""
	else
		s.Value = "No one has won!"
	end
	wait(3)
	local ingame = workspace.Ingame:GetChildren()
	for i =1,#ingame do
		local plr = game.Players:GetPlayerFromCharacter(ingame[i])
		plr:LoadCharacter()
		
	end
	workspace[curmap]:Destroy()
	end
2 Likes

That’s a lot of code! To make ti easier to read, can you format it correctly?

This is what is should look like:
HowToCode
Wrap your code in 3 ` on both ends.

Edit: Perfect!

1 Like

So, what do you mean by this? Is it that you want to the script to run only when there are a certain number of players?

1 Like

I want it to run if there are 2 players and I want to change the local s value to “Not enough players” if not. Do u understand?

1 Like

Hi. The condition you have to check is:

if #game:GetService("Players"):GetPlayers() >= 2 then
    --run the code fragment you want to start the game
else
    --change s to “Not enough players”
end
9 Likes

Ok I did just that but it doesnt work. I started the game in studio with 2 players and it showed “there are not enough players”.

Here`s what i did


local s = script.Stat
local vals = game.ReplicatedStorage.vals
t = 0

while true do
	if #game:GetService("Players"):GetPlayers() >= 2 then
	t = 20
	repeat
		t = t-1
		s.Value = "Intermission... "..t
		wait(1)
	until t == 0
	s.Value = "Game starting!"
	wait(2)
	local mapselect = game.ReplicatedStorage.Games:GetChildren()
	local choose = math.random(1,#mapselect)
	curnum = 0
	for i =1,#mapselect do
		curnum = curnum +1
		if curnum == choose then
			mapselect[i]:Clone().Parent = workspace
		curmap = mapselect[i].Name
			s.Value = "Map: "..mapselect[i].Name
		end
	end
	wait(3)
	local plrs = game.Players:GetChildren()
	for i = 1,#plrs do
		local num = math.random(1,32)
		plrs[i].Character.Head.CFrame = CFrame.new(workspace.Teleports["Part"..num].Position)
		plrs[i].Character.Parent = workspace.Ingame
	end
	t=100
	repeat
		t = t-1
		s.Value = t.." seconds left"
		wait(1)
	until t ==0 or vals.Winner.Value ~= ""
	if vals.Winner.Value ~= "" then
		s.Value = vals.Winner.Value.. " has won!"
		game.Players[vals.Winner.Value].leaderstats.Points.Value =game.Players[vals.Winner.Value].leaderstats.Points.Value +50
		game.Players[vals.Winner.Value].leaderstats.Wins.Value = 	game.Players[vals.Winner.Value].leaderstats.Wins.Value +1
		vals.Winner.Value = ""
	else
		s.Value = "No one has won!"
	end
	wait(3)
	local ingame = workspace.Ingame:GetChildren()
	for i =1,#ingame do
		local plr = game.Players:GetPlayerFromCharacter(ingame[i])
		plr:LoadCharacter()
		
	end
	workspace[curmap]:Destroy()
	else
		s.Value = "There are not enough players"
	end
	end

4 Likes

You should insert a “wait()” outside the main if statement or the game could freeze. Are you sure your script ran without errors? I replicated the script on Roblox Studio with a small delay and it worked as expected for 1 and for 2 players. Maybe the “Stat” value had the string “There are not enough players” by default and the script just did not change it.

Also, on line 27 it is recommended you call “GetPlayers” instead of “GetChildren” (who knows if there are other instances different from Players in there :stuck_out_tongue:)

3 Likes

Thanks man, I didnt insert the wait() first time and that was why. Anyway I love meeting people like you in our community! Keep doing that!