Scripting Helpers is winding down operations and is now read-only. More info→
Ad
Log in to vote
0

How to make an if statement that tests for how many players are on a certain team?

Asked by 6 years ago
Edited 6 years ago

I am making a minigame in Roblox, but there are some parts that aren't working, but I think they should be. Here's the code:

function setTeam(player, teamName)
    player.TeamColor = game.Teams[teamName].TeamColor
end
local minPlayers = 1
local players = game.Teams.Waiting:GetPlayers()
local playingplayers = game.Teams.Playing:GetPlayers()
local level1Id = 1475391820
print(players)
while true do
    wait(1)
    script.Parent.Parent.Workspace.Playing.Value=false
    if #players >= minPlayers - 1 then
        wait(10)
        print("Minigame Starting!")
        for _, player in pairs(game.Players:GetPlayers()) do
            setTeam(player, "Playing")
        end
        target = CFrame.new(-2.098, 25.025, 14.111)
        for i, player in ipairs(game.Players:GetChildren()) do
            if player.Character and player.Character:FindFirstChild("HumanoidRootPart") then
                player.Character.HumanoidRootPart.CFrame = target + Vector3.new(0, i * 5, 0)
            end
        end
        script.Parent.Parent.Workspace.Playing.Value=true
        wait(5)
        script.Parent.Parent.Workspace.GameSpawn.Transparency=1
        script.Parent.Parent.Workspace.GameSpawn.CanCollide=false
            if #playingplayers == 1 then
            script.Parent.Parent.Workspace.GameSpawn.Transparency=0
            script.Parent.Parent.Workspace.GameSpawn.CanCollide=true
            target = CFrame.new(45.64, 6.499, 13.96)
            for i, player in ipairs(game.Players:GetChildren()) do
            if player.Character and player.Character:FindFirstChild("HumanoidRootPart") then
                player.Character.HumanoidRootPart.CFrame = target + Vector3.new(0, i * 5, 0)
            end
            for i, v in pairs(game.Teams["Playing"]:GetPlayers()) do
                game.Players.LocalPlayer.leaderstats.Wins.Value = game.Players.LocalPlayer.leaderstats.Wins.Value + 1
            end
            end
            end
    elseif #players < minPlayers - 1 then
        print("Not Enough Players!")
    end
    wait(10)
    end

This is the part that isn't working:

if #playingplayers == 1 then
            script.Parent.Parent.Workspace.GameSpawn.Transparency=0
            script.Parent.Parent.Workspace.GameSpawn.CanCollide=true
            target = CFrame.new(45.64, 6.499, 13.96)
            for i, player in ipairs(game.Players:GetChildren()) do
            if player.Character and player.Character:FindFirstChild("HumanoidRootPart") then
                player.Character.HumanoidRootPart.CFrame = target + Vector3.new(0, i * 5, 0)
            end
            for i, v in pairs(game.Teams["Playing"]:GetPlayers()) do
                game.Players.LocalPlayer.leaderstats.Wins.Value = game.Players.LocalPlayer.leaderstats.Wins.Value + 1
            end
            end

When there is only one player in the "Playing" Team, then their score will go up by 1 and everyone in the game will be sent to the spawn point. But, for some reason, when the amount of players in the "Playing" team is equal to 1, then it does nothing. Would you please help me?

1 answer

Log in to vote
0
Answered by 6 years ago
Edited 6 years ago

To count players in a team is pretty easy

local teamRedPlayers = #game.Teams.Red:GetPlayers()

But I think your problem is that you only count it ONCE, you should add it to a loop so it keeps counting

function setTeam(player, teamName)
    player.TeamColor = game.Teams[teamName].TeamColor
end
local minPlayers = 1
local level1Id = 1475391820
while true do
    wait(1)
    script.Parent.Parent.Workspace.Playing.Value=false
    local players = game.Teams.Waiting:GetPlayers()
    local playingplayers = game.Teams.Playing:GetPlayers()
    if #players >= minPlayers - 1 then
        wait(10)
        print("Minigame Starting!")
        for _, player in pairs(game.Players:GetPlayers()) do
            setTeam(player, "Playing")
        end
        target = CFrame.new(-2.098, 25.025, 14.111)
        for i, player in ipairs(game.Players:GetChildren()) do
            if player.Character and player.Character:FindFirstChild("HumanoidRootPart") then
                player.Character.HumanoidRootPart.CFrame = target + Vector3.new(0, i * 5, 0)
            end
        end
        script.Parent.Parent.Workspace.Playing.Value=true
        wait(5)
        script.Parent.Parent.Workspace.GameSpawn.Transparency=1
        script.Parent.Parent.Workspace.GameSpawn.CanCollide=false
            if #playingplayers == 1 then
            script.Parent.Parent.Workspace.GameSpawn.Transparency=0
            script.Parent.Parent.Workspace.GameSpawn.CanCollide=true
            target = CFrame.new(45.64, 6.499, 13.96)
            for i, player in ipairs(game.Players:GetChildren()) do
            if player.Character and player.Character:FindFirstChild("HumanoidRootPart") then
                player.Character.HumanoidRootPart.CFrame = target + Vector3.new(0, i * 5, 0)
            end
            for i, v in pairs(game.Teams["Playing"]:GetPlayers()) do
                game.Players.LocalPlayer.leaderstats.Wins.Value = game.Players.LocalPlayer.leaderstats.Wins.Value + 1
            end
            end
            end
    elseif #players < minPlayers - 1 then
        print("Not Enough Players!")
    end
    wait(10)
    end
Ad

Answer this question