Why does it return #playersingame as 0?

I’m making a Color Block game, and wanted it stop the game when the players in game are lower than 2 and keep playing when the players in game are equal to or greater than 2, but it keeps returning as 0 even though there’s players in the game. Any fix?

Script:

game.ReplicatedStorage:WaitForChild("Status")
local Status = game.ReplicatedStorage.Status
local playersingame = {}
local playeri
local intermission
local gamehasbegun
function intermission()
	table.clear(playersingame)
	for _, v in pairs(game.Players:GetChildren()) do
		if v:FindFirstChild("Playing") then
			if v.Playing.Value == true then
				v.Playing.Value = false
			end
		end
	end
	if #game.Players:GetChildren() < 2 then
		repeat
			Status.Value = "Need "..(2 - #game.Players:GetChildren()).." more player(s).."
			wait()
		until #game.Players:GetChildren() >= 2
	end
	Status.Value = "Intermission.. (10)"
	local t = 10
	wait(1)
	repeat
		t = t - 1
		Status.Value = "Intermission.. ("..t..")"
		wait(1)
	until t == 1
	Status.Value = "Beginning round.."
	wait(3)
	Status.Value = "Game has begun"
	gamehasbegun()
end
function gamehasbegun()
	local t = 5
	for _, v in pairs(game.Players:GetChildren()) do
		if v:FindFirstChild("Playing") then
			if v.Playing.Value == false then
				v.Playing.Value = true
			end
		end
	end
	for _, v in pairs(game.ReplicatedStorage.ColorParts:GetChildren()) do
		v.BrickColor = BrickColor.Random()
	end
	game.ReplicatedStorage.PartColor.BrickColor = BrickColor.Random()
	game.ReplicatedStorage.ColorParts:Clone().Parent = game.Workspace
	for _, v in pairs(game.Players:GetChildren()) do
		if v.Character:FindFirstChild("Humanoid") and v.Character:FindFirstChild("HumanoidRootPart") then
			v.Character.HumanoidRootPart.CFrame = game.Workspace.TowerInvisibleWalls.Top.CFrame - Vector3.new(0, 5, 0)
		end
	end
	wait(5)
	while wait(t) do
		local numberpos = 0
		for _, player in pairs(game.Players:GetChildren()) do
			if player.Playing == true then
				if table.find(playersingame, player.Name) then
					print("already found")
				else
					numberpos = numberpos + 1
					table.insert(playersingame, numberpos, player.Name)
				end
			else
				if table.find(playersingame, player.Name) then
					for i, v in pairs(playersingame) do
						if v == player.Name then
							playeri = i
						end
					end
				end
			end
		end
		if #playersingame < 2 then
			print(#playersingame)
			for _, v in pairs(game.Players:GetChildren()) do
				if v.Character:FindFirstChild("Humanoid") and v.Character:FindFirstChild("HumanoidRootPart") then
					v.Character.HumanoidRootPart.CFrame = game.Workspace.SpawnLocations.MainSpawn.CFrame + Vector3.new(0, 1, 0)
				end
			end
			if #playersingame == 1 then
				print(#playersingame)
				for _, v in pairs(playersingame) do
					game.Players[v].leaderstats.Cash.Value = game.Players[v].leaderstats.Cash.Value + 100
					game.ReplicatedStorage.Status.Value = v.." has won the game!"
				end
			end
			wait(5)
			intermission()
			break
		end
		if #game.Players:GetChildren() < 2 then
			for _, v in pairs(game.Players:GetChildren()) do
				if v.Character:FindFirstChild("Humanoid") and v.Character:FindFirstChild("HumanoidRootPart") then
					v.Character.HumanoidRootPart.CFrame = game.Workspace.SpawnLocations.MainSpawn.CFrame + Vector3.new(0, 1, 0)
				end
			end
			intermission()
			break
		end
		print(#game.Players:GetChildren().." players in total.")
		print(#playersingame.." players in game.")
		game.ReplicatedStorage.PartColor = BrickColor.Random()
		local colorpart = false
		for _, v in pairs(game.Workspace.ColorParts:GetChildren()) do
			if colorpart == false then
				if v.BrickColor == game.ReplicatedStorage.PartColor.BrickColor then
					print("good")
					colorpart = true
				else
					print("bad")	
				end
			end
		end
		if colorpart == false then
			local randompart = game.Workspace.ColorParts[math.random(1, #game.Workspace.ColorParts)]
			randompart.BrickColor = game.ReplicatedStorage.PartColor.BrickColor
		end
		wait(t)
		for _, v in pairs(game.Workspace.ColorParts:GetChildren()) do
			if v.BrickColor ~= game.ReplicatedStorage.PartColor.BrickColor then
				v:Destroy()
			end
			wait()
		end
	end
end

wait(5)
intermission()

Here’s the code i’m suspecting is messing it up:

	while wait(t) do
		local numberpos = 0
		for _, player in pairs(game.Players:GetChildren()) do
			if player.Playing == true then
				if table.find(playersingame, player.Name) then
					print("already found")
				else
					numberpos = numberpos + 1
					table.insert(playersingame, numberpos, player.Name)
				end
			else
				if table.find(playersingame, player.Name) then
					for i, v in pairs(playersingame) do
						if v == player.Name then
							playeri = i
						end
					end
				end
			end
		end

Remove the numberpos argument, shouldn’t fix anything though.

1 Like

Hmm, I can’t see an obvious issue from looking at the code, and I don’t think you’re setting the table wrong (I could be wrong though). Maybe try changing game.Players:GetChildren() to game.Players:GetPlayers(), that’s only potential fix I could find from a quick glance at the code.

On a side-note, it’s recommended to use game:GetService to get services instead of directly getting them through game. That’s not causing the issue though, you don’t have to change that.

Thats not it.

if #game.Players:GetChildren() < 2 then
		repeat
			Status.Value = "Need "..(2 - #game.Players:GetChildren()).." more player(s).."
			wait()
		until #game.Players:GetChildren() >= 2
	end

Whenever I join the game without 2 players it says I need 1 more player(s) and when my friend joins the game it goes to the next code and does the game.

its better to use game.Players:GetPlayers() instead of game.Players:GetChildren() just to point that out

My only guess is you didn’t mention the value, only the instance. As in, I’m assuming “player.Playing” is a boolean inside a player which tells if a player is playing or not, but you aren’t refering to the value of the boolean.

2 Likes

Lol why not just do

#game.Players:GetPlayers() – that returns how much plrs are in game.

1 Like

No, its a color block minigame. I wan’t to return the players in the minigame not in the game.

OP has a boolean inside each player which tells if the player is actually playing the game or not (I suppose), they need valid players

1 Like

Yes, that’s where the directory of the values are.

Why not using a Folder in workspace called InGame or Playing.
If character dies it gets parented to workspace again anyway.

#workspace.Playing:GetChildren()
that would be simple as hell

and then you would be easily able to add a plr.Afk.Value == true then return end so it doesn’t parent him to Playing :slight_smile:

If I did that now, I’d have to re-script EVERYTHING.

for _, player in pairs(game.Players:GetChildren()) do
    if player.Playing == true then
    end
end

You aren’t checking the value, change “player.Playing” to “player.Playing.Value” and see if it solves your issue

4 Likes

Not really,

Koreacty is correct, or else he isn’t referencing the value correctly.

3 Likes