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

How to Handle Getting Team Players?

Asked by 5 years ago
Edited 5 years ago

So I've made a script that works along with my Datastore but I'm getting a error called WinningTeamName is not a valid member of Teams.

    local teams = game:GetService("Teams")

    game.ServerStorage.PlayerWin.Event:Connect(function()

local WinningTeam = script.WinningTeam.Value
local WinningTeamName = teams:FindFirstChild(WinningTeam)
local WinningPlayers = teams.WinningTeamName:GetPlayers()

for _, Player in pairs(teams[WinningTeamName]:GetPlayers()) do

    local User = Player.UserId
    local PlayerData = game.ServerStorage:FindFirstChild(User)

    PlayerData.ELO.Value = PlayerData.ELO.Value + 10

end

    end)
0
Use '[' not '{'. So do "teams[WinningTeamName]" on line 09 Troxure 87 — 5y
0
@Troxure. I tried that but it didnt work ZentuzFrost 5 — 5y

1 answer

Log in to vote
0
Answered by
Azarth 3141 Moderation Voter Community Moderator
5 years ago
Edited 5 years ago

If you're using FindFirstChild() then you're making sure something is there. While using the method will initially prevent errors, it does nothing if you don't check it with an if statement.

-- Index all of your static variables outside of the event scope
local teams = game:GetService("Teams")
local ServerStorage = game:GetService("ServerStorage")
local PlayersService = game:GetService('Players')

local PlayerWinEvent = ServerStorage:WaitForChild("PlayerWin")
local WinningTeamValue = script:WaitForChild('WinningTeam')

PlayerWinEvent.Event:Connect(function()
    local WinningTeam = teams:FindFirstChild(WinningTeamValue.Value)    
    if WinningTeam then 
        for _, Player in pairs(PlayersService:GetPlayers()) do
            -- Is Player's team the winning Team?
            if Player.Team == WinningTeam then 
                local PlayerUserId = Player.UserId
                local PlayerData = ServerStorage:FindFirstChild(PlayerUserId)
                -- if PlayerData exists
                if PlayerData then
                    local ELO = PlayerData:FindFirstChild("ELO")
                    -- if ELO exists
                    if ELO then 
                        ELO.Value = ELO.Value + 10
                    else 
                        print ( "Elo value does not exist for ", Player.Name )
                    end
                else
                    print ( Player.Name, 'does not exist in ServerStorage' )
                end
            end
        end
    else
        print ( WinningTeamValue.Value, 'does not exist' )
    end
end)

Ad

Answer this question