I have a round based game, that at the end, I want the highest person to get points. How would I do this? Here is the script:
for _, v in pairs(game.Players:GetChildren()) do local Kills = game.Players[v.Name].leaderstats.Kills.Value print(Kills) end
You can simply use a variable to check if the value of the player is greater than the value of the last player, so until it reaches the highvalue
Example:
local high = 0 for _,v in pairs(game.Players:GetPlayers()) do if v.leaderstats.Money.Value > high then high = v.leaderstats.Money.Value end end print("High value is: " .. tostring(high))
For your script you can use this:
local high = 0 for _,v in pairs(workspace.Players:GetChildren()) do local player = game.Players:FindFirstChild(v.Name) -- Player variable if player then -- Check if player exist local Kills = player.leaderstats.Kills -- Value location if Kills.Value > high then -- Check if kills of player is greater than high high = Kills.Value -- Set the new value end end end print("The high value is: " .. tostring(high))
Hope it helped :)
we simply see which is greater the current highest value or the new value also v is the player
local highest for _, v in pairs(workspace.Players:GetChildren()) do if highest then if v.leaderstats.Kills.Value > highest then highest = v.leaderstats.Kills.Value end else highest = v.leaderstats.Kills.Value end end
While User yHasteeD answered your question, if you want all the player's values to be saved in a table and show a leaderboard with the highest kills player's stats are at the top, just simply
UPDATE: I was dumb when doing this, sorry.
local high = 0 local highBak= 0 local table= {} for _,v in pairs(workspace.Players:GetChildren()) do local player = game.Players:FindFirstChild(v.Name) -- Player variable if player then -- Check if player exist local Kills = player.leaderstats.Kills.Value -- Value location if Kills.Value > high then -- Check if kills of player is greater than high high = Kills.Value -- Set the new value end if Kills.Value >= highBak then -- Check if kills of player is greater than high table[#table+1]= player --Places the player object into a table value highBak= Kills.Value end end end print("The high value is: " .. tostring(high)) for i,v in pairs(table) do if v then -- Check if player exist local Kills = player.leaderstats.Kills.Value -- Value location print(v.Name.. "is #".. i.. " with ".. Kills.Value.. " Kills!") -- Prints players via their rank in the match. end end