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

Can you help me with player stat comparison?

So I have an array that takes the players in the game. I have a generic for loop, with i = 1 being, well, one player. I need to know how to compare it to the rest of the players.

local players = {}
for a,b in pairs(game.Players:GetChildren()) do
	table.insert(players,b)
end

for i = 1, #players do
	if players[i].leaderstats.KOs.Value > players[i].leaderstats.KOs.Value then

I need to find a way to compare player i's Kos to the rest of the players' individually. I am trying to see who gets the most kills. Thanks in advance :)

1 answer

✓ Accepted answer

Well to find who has the most kills, it is actually pretty simple. It is similar to what you were doing except, this time we need to keep track of the information through the use of variables.

function getHighestKills()
	local kills = 0 
	local player = nil
	
	for i,v in pairs(game.Players:GetPlayers()) do
		local kos = v.leaderstats.KOs.Value
		if kos > kills then
			kills = kos
			player = v
		end
	end
	return player, kills
end

To use it, simply do this:

local player, kills = getHighestKills()
print(player.Name, " ",  kills)

2 comments

0

Hi :) Thanks for the help! It didn't work though, as when I put it as a function, it said getHighestKills() was a nil global. I tried replacing my for loop with it, removing the return and replacing it with *print:(player)", and it didn't work. Thanks :)

0

Can you show me how you laid out your script?

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