Scripting Helpers is winding down operations and is now read-only. More info→
Ad
Log in to vote
0

How do I use for _,v in pairs loop to order everyones kills and find the highest one?

Asked by 5 years ago
Edited 5 years ago

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
0
I posted an answer, you can see the answer? yHasteeD 1819 — 5y

3 answers

Log in to vote
2
Answered by
yHasteeD 1819 Moderation Voter
5 years ago
Edited 5 years ago

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

Ad
Log in to vote
0
Answered by 5 years ago
Edited 5 years ago

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
0
This is wrong, you are not picking up the player directory and also, it is not necessary to do all that you did. yHasteeD 1819 — 5y
0
i did the same thing you did mattchew1010 396 — 5y
0
no, the "workspace.Players" is player names, not players yHasteeD 1819 — 5y
0
i thought it was game.Players oops mattchew1010 396 — 5y
0
i just copied theirs mattchew1010 396 — 5y
Log in to vote
0
Answered by 5 years ago
Edited 5 years ago

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
0
You can improve upon this script, and have it even more accurate, as the script wont put players who have the same amount of kills into the same value popgoesme700 113 — 5y

Answer this question