Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
✓ ANSWER ACCEPTED

How to prevent the game starting if there is not enough players?

So I've been working on this script that has a round, intermission, etc. But what I'm trying to make is it so that there HAS to be at least 2 players to start the round. I've tried looking everywhere and can't seem to find anything.

This is the script:

local replicatedstorage = game:GetService("ReplicatedStorage")
local status = replicatedstorage:WaitForChild("StatusValue")

while true do
	Song = Instance.new("Sound")
	Song.Parent = workspace
	Song.Volume = 1
	Song.Looped = false
	Assets = {274504661,346191865,263966756,220964582,362105653}
	for _, asset in ipairs(Assets) do
	game:GetService("ContentProvider"):Preload("http://www.roblox.com/asset/?id=" .. asset)
end
	Song.SoundId = "http://www.roblox.com/asset/?id=" ..Assets[math.random(1,#Assets)]
	Song:Play()
-- Intermission
	for i = 30,0,-1 do
		status.Value = "INTERMISSION: "..i
		wait(1)
		
end

-- Round
status.Value = "CHOOSING THE MAP."
wait(0.5)
status.Value = "CHOOSING THE MAP.."
wait(0.5)
status.Value = "CHOOSING THE MAP..."
wait(0.5)
local num = math.random(1,3)
if num == 1 then
	game.Lighting.Map1:Clone().Parent = game.Workspace
	status.Value = "GRAVEYARD MAP CHOSEN! MAP BY: SW00N"
	target = CFrame.new(346.52, 358.91, -4)
for i, player in ipairs(game.Players:GetChildren()) do
   if player.Character and player.Character:FindFirstChild("Torso") then
      player.Character.Torso.CFrame = target + Vector3.new(0, i * 0, 0)
   end
end
end
if num == 2 then
	game.Lighting.Map2:Clone().Parent = game.Workspace
	status.Value = "VALCANO MAP CHOSEN! MAP BY: SW00N"
	target = CFrame.new(457.269, 358.51, 22.469)
for i, player in ipairs(game.Players:GetChildren()) do
   if player.Character and player.Character:FindFirstChild("Torso") then
      player.Character.Torso.CFrame = target + Vector3.new(0, i * 0, 0)
   end
end
end
if num == 3 then
	game.Lighting.Map3:Clone().Parent = game.Workspace
	status.Value = "TRAIN MAP CHOSEN! MAP BY: SW00N"
	wait(0.5)
	target = CFrame.new(480.219, 361.119, 182.664)
for i, player in ipairs(game.Players:GetChildren()) do
   if player.Character and player.Character:FindFirstChild("Torso") then
      player.Character.Torso.CFrame = target + Vector3.new(0, i * 0, 0)
   end
end
end


for _, player in pairs(game.Players:GetPlayers()) do
	if player.Character then
		player.Character.Humanoid.WalkSpeed = 0
	end
end

status.Value = "CHOOSING THE SNIPER."
wait(0.3)
status.Value = "CHOOSING THE SNIPER.."
wait(0.3)
status.Value = "CHOOSING THE SNIPER..."
wait(0.3)
Song:Stop()
local players = game.Players:GetChildren()

    print(#players)


 if #players >= 2 then

        local sniper = players[math.random(1,#players)]
    local chosenName = sniper.Name
        local weapon = game.Lighting.Sniper
    sniper.Character.Torso.CFrame = game.Workspace:FindFirstChild("SniperPosition").CFrame * CFrame.new(0,1,0)

game.Workspace.Countdown:Play()
status.Value = "5"
wait(1)
status.Value = "4"
wait(1)
status.Value = "3"
wait(1)
status.Value = "2"
wait(1)
status.Value = "1"
wait(1)
game.Workspace.Begin:Play()
weapon:Clone().Parent = sniper.Backpack
status.Value = "BEGIN!"
wait(1)

for _, player in pairs(game.Players:GetPlayers()) do
	if player.Character then
		player.Character.Humanoid.WalkSpeed = 16
	end
end
game.Workspace.Round:Play()
for i = 60,0,-1 do
	status.Value = "TIME LEFT: "..i
	wait(1)
end
game.Workspace.Round:Play()
status.Value = "GAME OVER"
game.Workspace.Round:Stop()
wait(1)

	end
end

1 comment

0

I'll create something to show you how to prevent the game from starting, but this will be a completely basic version and you will have to implement it into your script accordingly.

1 answer

✓ Accepted answer

Script is located in ServerScriptService and there is a NumberValue inside of it named "NumOfPlayers".

This script will only show you how to keep track of player count (atleast I hope so, haven't scripted in forever), so you should be able to implement it into your own script using the statuses of the NumOfPlayer value. Good luck.

local Players = game:GetService("Players") -- Gets Player, same thing as 'game.Players'

Players.PlayerAdded:connect(function(P) -- When a player is addedd...
	local Char = game.Workspace:WaitForChild(P.Name) -- Wait for Character to spawn
	if Char then -- if Character is spawned then...
		script.NumOfPlayers.Value = script.NumOfPlayers.Value+1 -- Increase the NumValue
		print(P.Name.." has entered") 
		print("Player count is now "..script.NumOfPlayers.Value)
	end
end)

Players.PlayerRemoving:connect(function(P) -- When a player leaves
	script.NumOfPlayers.Value = script.NumOfPlayers.Value-1 -- Decrease the NumValue
	print(P.Name.." has left")
	print("Player count is now "..script.NumOfPlayers.Value)
end)

1 comment

0

There's also a value in Players called NumPlayers OR you could just use :GetChildren() do get the count.

This discussion is preserved as it was. New answers, comments, and votes are closed.