Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
COMMUNITY QUESTION

How to Handle Getting Team Players?

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)

2 comments

0

Use '[' not '{'. So do "teams[WinningTeamName]" on line 09

0

@Troxure. I tried that but it didnt work

1 answer

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)

This discussion is preserved as it was. New answers, comments, and votes are closed.