How do you access the team leaderstats?

I’m currently trying to create a capture the flag type game. I want to access the team leaderstats that show up on the top-right player leaderstats. I’m unsure how I would check which team wins the game without knowing the whole teams leaderstats. Any idea on how you would figure this out? I’ve looked on another post but all the ideas weren’t suitable or didn’t work.

Refer to this.
Little issue with Team Points

edit: Your script should contain and measure if one of the team’s have 1 win/point.

Create a folder and add a Value to it.


game.Players.PlayerAdded:Connect(function(plr)
	local leaderstats = Instance.new("Folder", plr)
	leaderstats.Name = "leaderstats"
	
	local Coins = Instance.new("IntValue")
	Win.Name = "Win"
	Win.Value = 0
	Win.Parent = leaderstats
end)


local Players = game.Players

local W = true
local LeaderStats =  Player:FindFirstChild("leaderstats")

		if LeaderStats then

			local Win = LeaderStats:WaitForChild("Win")

			Win.Value += 1

              elseif Win.Value >= 2 then

                        Win.Value -= 1


			task.wait(1)
			W = true
                  end 
            end
      end
end)
1 Like

You can’t, I’ll recommend getting a table every player that’s in the specified team, then counting the stats.

For the table, you can use Team:GetPlayers(), being this the simplest way of getting all players.

local Count = 0 -- Stat count
for i, Player in pairs(Team:GetPlayers()) do -- We get every player that's in a specified team
Count += Player.leaderstats["stat"].Value -- Now we add up the stat value to the count
end

print(Count) -- Final stat count

You can do this on a constant while loop to keep updating the count

Hope you find this helpful

2 Likes

I was thinking of something like this but never figured out how to do it. Thanks. But there’s one thing I forgot to mention. How would you specify it for each team? Instead of just this for 1 team. There are 2 teams in the game, so it would need to print both teams’ total values.

1 Like
local Game = game
local Teams = Game:GetService("Teams")

local function GetData()
	local Data = {}
	for _, Team in ipairs(Teams:GetTeams()) do
		Data[Team] = {}
		for _, Player in ipairs(Team:GetPlayers()) do
			Data[Team][Player] = {}
			local Leaderstats = Player:FindFirstChild("leaderstats")
			if not Leaderstats then continue end
			for _, Child in ipairs(Leaderstats:GetChildren()) do
				if Child:IsA("ValueBase") then Data[Team][Player][Child] = true end
			end
		end
	end
	return Data
end

local Data = GetData()
for Team, TeamData in pairs(Data) do
	for Player, PlayerData in pairs(TeamData) do
		for Stat, _ in pairs(PlayerData) do
			print(Team.Name, Player.Name, Stat.Name, Stat.Value)
		end
	end
end
1 Like