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)
: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)
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!