How to give top 5 leaderstats chat tags

I already am saving the leaderstats. I just need a way to check if a player holds the top 5 highest position, and to give that person a chattag.

This i my current code:

local t2 = {}
game.Players.PlayerAdded:Connect(function()
 table.Clear(t2)
 local t1 = game.Players.GetChildren()
 for i = 1, 5, 1 do
  local Higest = 0
  local Plr
  for i, v in pairs(t1) do
   if game.Players:FindFirstChild(v).leaderstats.Coins.Value > Higest then --replace "Coins" with your leaderstats value
    Higest = game.Players:FindFirstChild(v).leaderstats.Coins.Value
    plr = v
   end
  end
  table.Remove(t1, plr)
  table.Insert(t2, plr)
 end
end)


local Players = game:GetService("Players")
local ChatService = require(game.ServerScriptService:WaitForChild("ChatServiceRunner"):WaitForChild("ChatService"))
Players.PlayerAdded:Connect(function(plr)
    ChatService.SpeakerAdded:Connect(function(player)
    local Speaker = ChatService:GetSpeaker(player)
    local Id = Players:GetUserIdFromNameAsync(player)
    local plr = Players:FindFirstChild(player)
    if table.Find(t2, plr) then
     Speaker:SetExtraData('Tags', {{TagText = "Top 5", TagColor = Color3.fromRGB(255, 0, 0)}})
    end
end)

but this is the error i get

:FindFirstChild() takes as first argument a string, not a player instance

local t2 = {}
game.Players.PlayerAdded:Connect(function()
 table.Clear(t2)
 local t1 = game.Players.GetChildren() -- array of instances (usually just player instances)
 for i = 1, 5, 1 do
  local Higest = 0
  local Plr
  for i, v in pairs(t1) do -- loop through the array of players
   -- i (in this case) is a number, v is a player
   if game.Players:FindFirstChild(v--[[ v is a player not a string ]]).leaderstats.Coins.Value > Higest then --replace "Coins" with your leaderstats value
    Higest = game.Players:FindFirstChild(v).leaderstats.Coins.Value
    plr = v
   end
  end
  table.Remove(t1, plr)
  table.Insert(t2, plr)
 end
end)

You can just do v.leaderstats.Coins.Value

It is not a player instance, the SpeakerAdded event’s parameter is the SpeakerName (a string).

leaderstats is nil.
I’m pretty sure you store your leaderstats inside the player instance, so you need to do the following:

Player:FindFirstChild('leaderstats')

image
means that you’re trying to do nil.leaderstats

Here :FindFirstChild() returns nil because it cannot find a child with the name v (a Player instance)

v.Name would be right, just to add onto this comment.

But you could make it even easier by doing v:FindfirstChild('leaderstats')

I just noticed that you do .SpeakerAdded:Connect() everytime a player joins, which means that if there are 2 players, that code will run thrice for the next player!