Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
✓ ANSWER ACCEPTED

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

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

1 comment

0

I posted an answer, you can see the answer?

3 answers

✓ Accepted answer

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

5 comments

0

This is wrong, you are not picking up the player directory and also, it is not necessary to do all that you did.

0

i did the same thing you did

0

no, the "workspace.Players" is player names, not players

0

i thought it was game.Players oops

0

i just copied theirs

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

1 comment

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

This discussion is preserved as it was. New answers, comments, and votes are closed.