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