Hey! I am trying to make that all players have a value inside and who ever has the highest kills on the classic leaderboard gets their value set to true while others are set to false. Is there a way to set that up and as a loop?
Yes, here is an example of it in a function. I’ve added plenty of checks to see if the required items exist however that may not be needed dependant on how exactly you’re using this.
Highly recommend cleaning this up too, wrote this on mobile.
local function FindHighestKills()
local HighestValue = -1
local HighestPlayer;
for i, plr in pairs(game.Players:GetPlayers()) do
if plr:FindFirstChild("BoolValue") then -- change this as well as all other instances of "BoolValue" to the name of it
plr.BoolValue.Value = false -- assuming you want to set everyones current BoolValue to false, to prevent multiple people from having it
if plr:FindFirstChild("leaderstats") then
if plr.leaderstats:FindFirstChild("Kills") then -- all these findfirstchilds might be kinda redundant if you already know the player has these values, however i decided to add it anyway just to be safe.
if plr.leaderstats.Kills.Value > HighestValue then
HighestPlayer = plr
HighestValue = plr.leaderstats.Kills.Value
end
end
end
end
end
if HighestPlayer then
HighestPlayer.BoolValue.Value = true
end
end
FindHighestKills() -- call this whenever you want to ref who has the highest kills
local players, info = game:GetService("Players"), {
boolean = false,
highestuser = nil,
highestvalue = 0
}
local myuserid = players.LocalPlayer.UserId
function gethighestvalue(keyname)
local newInfo = {
boolean = false,
highestuser = nil,
highestvalue = 0
}
for i, plr in pairs(players:GetPlayers()) do
local leaderstats = plr:FindFirstChild("leaderstats"); -- can change the location
local keyvalue = leaderstats:FindFirstChild(keyname); -- can change the location
newInfo.boolean = (keyvalue.Value>newInfo.highestvalue) and (plr.UserId==myuserid)
if (keyvalue.Value>newInfo.highestvalue) then
newInfo.highestvalue = keyvalue.Value;
newInfo.highestuser = plr;
end
end
return newInfo;
end