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

The elseif section doesn't work. Why?

Asked by 8 years ago

Hi guys! This loop is part of an Elimination round for a Murder Game. I tested on studio, the first "if" works, but when there's only 1 player in the "Playing" team, the "elseif" section should kick in but it doesn't. It seems like I went wrong somewhere. There are no output errors. Please help.

THE CODE IS SOMEWHERE BETWEEN 50 and 60 LINES LONG

while wait()  do
        players = game.Players:GetPlayers()
        if game.Workspace.Time.Value == 0 or game.Players.NumPlayers <2 then
            workspace.Ingame.Value = false
            workspace.Elimination.Value = false
            for i=1,#players do
                if game.Players:FindFirstChild(players[i]) then
                items = players[i].Backpack:GetChildren()
                for b = 1,#items do
                    if items[b]:IsA("Tool") then
                        items[b]:Destroy()
                    end
                end
                tools = players[i].StarterGear:GetChildren()
                for v=1,#tools do
                    tools[v]:Destroy()
                end
                players[i].TeamColor = game.Teams["Not Playing"].TeamColor
                wait(0.5)
                players[i]:LoadCharacter()
                end
            end
            game.Workspace:FindFirstChild("Map"):Destroy()
            game.Workspace.Lobby.Noti.SurfaceGui.TextLabel.Round.Text = ""
            game.Workspace.Lobby.Noti.SurfaceGui.TextLabel.Map.Text = ""
            break
        elseif game.Teams.Playing:GetChildren() == 1 then
            winner = game.Teams.Playing[1].Name
            workspace.Ingame.Value = false
            workspace.Elimination.Value = false
            game.Workspace.Time.Value = 0
            for i=1,#players do
                if game.Players:FindFirstChild(players[i]) then
                items = players[i].Backpack:GetChildren()
                for b = 1,#items do
                    if items[b]:IsA("Tool") then
                        items[b]:Destroy()
                    end
                end
                tools = players[i].StarterGear:GetChildren()
                for v=1,#tools do
                    tools[v]:Destroy()
                end
                players[i].TeamColor = game.Teams["Not Playing"].TeamColor
                wait(0.5)
                players[i]:LoadCharacter()
                end
            end
            game.Workspace:FindFirstChild("Map"):Destroy()
            game.Workspace.Lobby.Noti.SurfaceGui.TextLabel.Round.Text = ""
            game.Workspace.Lobby.Noti.SurfaceGui.TextLabel.Map.Text = ""
            break
        end
end

2 answers

Log in to vote
1
Answered by
1waffle1 2908 Trusted Badge of Merit Moderation Voter Community Moderator
8 years ago

You wrote game.Teams.Playing:GetChildren() == 1 which is false. GetChildren returns a table, which is not equal to a number. Teams also do not store players, because that's what Players does. On line 28 you wrote game.Teams.Playing[1] which doesn't make sense because it is not a table, it is an object, and you are trying to index a number from an object that has no children.

Here's a function to use instead of pretending that players are inside of teams objects:

function GetPlayersOnTeam(color)
    local t={}
    for _,v in pairs(game.Players:GetPlayers())do
        if v.TeamColor==color then
            t[#t+1]=v
        end
    end
    return t
end
local players = game.Players:GetPlayers()
local playing=GetPlayersOnTeam(game.Teams.Playing.TeamColor)
...

elseif #playing==1 then
    winner=playing[1]
...
0
I really like your answer, but someone has already answered on the community chat. Thanks for the help! fahmisack123 385 — 8y
Ad
Log in to vote
0
Answered by
adark 5487 Badge of Merit Moderation Voter Community Moderator
8 years ago

The Teams folder does not store the Players currently on that team, so using GetChildren() on it will return an empty Table. Even it is did give you what you expect it to, you have to use the # operator to get the count of Players in the Table, as {...} ~= 1 in any case.

What you need to do is make a function to count the Players on a given team properly:

function countPlayersInTeam(team)
    local tc = team.TeamColor
    local count = 0
    for _, player in ipairs(game.Players:GetPlayers()) do
        if player.TeamColor == tc then
            count = count + 1
        end
    end
    return count
end

and then change line 27 in that code you pasted above:

            elseif countPlayersInTeam(game.Teams.Playing) == 1 then

Answer this question