How to start a script at 2 players

How would i be able to start the script below when theres atleast 2 players in the game and also update the status bar with for example “1 more player needed to start”?.

local status = game.ReplicatedStorage.Status
local maps = game.ReplicatedStorage.Maps:GetChildren()

while true do
	for i = 1,15 do
		status.Value = "Next round will start in: "..15-i
		wait(1)
	end
	
	local rand = math.random(1, #maps)
	
	local map = maps[rand]:Clone()
	map.Parent = workspace
	
	status.Value = "The next minigame is: "..map.Name
	wait(5)
	
	
	local players = game.Players:GetChildren()
	for i = 1,#players do
		if players[i].Character ~= nil then
			local spawnLocation = math.random(1,#map.Teleports:GetChildren())
			players[i].Character:MoveTo(map.Teleports:GetChildren()[spawnLocation].Position)
			players[i].Character.Parent = workspace.Ingame
			
		end
	end
	
	local roundLenght = 60
	local canWin = true
	local roundType = ""
	
	if map:FindFirstChild("Race") then
		roundType = "Race"
		map.WinnerPart.Touched:Connect(function(hit)
			if hit.Parent:FindFirstChild("Humanoid") and canWin == true then
				canWin = false
				status.Value = hit.Parent.Name.." Has won!"
				
				local plr = game.Players:GetPlayerFromCharacter(hit.Parent)
				plr.leaderstats.Wins.Value = plr.leaderstats.Wins.Value +1
				plr.leaderstats.Cash.Value = plr.leaderstats.Cash.Value +100
			end
		end)	
	end
	
	repeat
		roundLenght = roundLenght -1
		status.Value = "Time left: "..roundLenght
		wait(1)
	until roundLenght == 0 or canWin == false or #workspace.Ingame:GetChildren() == 0
	
	wait(3)
	map:Destroy()	
	
	local players = game.Players:GetChildren()
	for i = 1,#players do
		if players[i].Character ~= nil then
			players[i]:LoadCharacter()
		end
	end
	
end

3 Likes

This won’t work because it you try to get the length of game.Players and not the actual players. What OP has to do is:

local Players = game:GetService("Players")
repeat task.wait(1) 
    print("Waiting for players:", tostring(#Players:GetPlayers()).."/2")
until #Players:GetPlayers() >= 2
1 Like

i get this error if i use that line of code

1 Like

Try what I gave you, it works.

1 Like

I get this error then, excuse me im just a few months into scripting and i dont really understand those errors yet

1 Like

yo try

local counter = 0
Players.PlayerAdded:Connect(function(player)
	counter += 1
	if counter > 2 then
		--Do whatever you want here (or use one of those funi remote event things to activate another script, or you can use otherscript.Disabled = false and have it set as disabled by default)
	end
end)

btw put this in server script storage if I remember correctly

2 Likes

Here I made a typo, try the script in my reply now!

1 Like

it stil starts if i use this ^^

1 Like

Try my script now, put the Players variable all the way on top of the script BTW

1 Like

It works now thanks, so then my second question from the topic, how would i be able to update the status bar with "waiting for players?

"

1 Like
repeat task.wait()
print("Waiting For Players...")
until #game.Players:GetPlayers() > = 2
1 Like

Just set the text instead of having the print:

1 Like

Did you literally just paste this into a script? because the part I commented out requires you to edit so it would actually start the original script in some ways.

For example, you could set your original script to “Disabled = true”, then add “game.Workspace.OriginalScript.Disabled = false” in the commented out area

So your script:

local Players = game:GetService("Players")
repeat task.wait(1) 
    status.Value = "Waiting for players: "..tostring(#Players:GetPlayers()).."/2"
until #Players:GetPlayers() >= 2

ServerScript :

local Rp = game:GetService("ReplicatedStorage")
local Status = Rp.Status
Status.Value = "Waiting For Players"
repeat task.wait() until #game.Players:GetPlayers() >= 2
if #game.Players:GetPlayers() >= 2 then
Status.Value = "Starting"
print("yes")
end

and then create a local script inside the textlabel and detect whenever the value changes if it changes then it will set the text to the StringValue’s Value.

LocalScript :

game.ReplicatedStorage.Status.Changed:Connect(function(Val)
script.Parent.Text = Val
end)

He all ready has that if you look at his script, that’s why all he has to do is hide what I gave him:

image

did i make a typo? it still gives a red line and doesnt work

Try it again, or just replace the comma (,) with two dots (…) use the script again, I edited it

Yes i already have a string value :slight_smile:

I replaced it with dots, no errors now but it still doesnt change the Statusbar, it just says Label i have no idea why