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 compare leaderstats in a server to find the player with the highest stat?

Hello, i am working on a game and have decided to implement an NPC which will take appearance of a player with the highest cash in the server.

Currently i am using the "GetCharacterAppearanaceAsync()" function. This function works and i have made it take appearance of me at the moment but i'd like this npc to take appearance of the player as mentioned in the title of this question.

I know how to find userIDs from usernames but what im stuck on is the comparing of the leaderstats; Do i use a linear search/Binary search/etc and if so how would i code it to connect with the leaderstats to find the highest score then match that score to the player with it?

In this scenario i am wanting the Cash to be compared considering this is the only stat i currently have on my game as of so far.

Thankyou very much for taking your time to read this :P

1 answer

✓ Accepted answer

*Note: This probably is an inefficient method. *

What you could do, is have a loop going through all players and updating who has the highest value. For example, if you want to check the cash value, you could do something like this:

local TopPlayer
local TopCash = 0

for i, plr in ipairs(game:GetService("Players")) do
	local Cash = plr.leaderstats.Cash.Value
	if Cash >= TopCash then -- Player has more or equal cash | This will cause issues in cases where there's players with the same amount of cash
		TopPlayer = plr -- Set the player as the top player
		TopCash = Cash
	end
end

-- Do whatever with the top player

This makes a loop going through all players and setting the player with the most cash as the TopPlayer variable.

11 comments

0

Yeah, that is kinda what i was thinking but i tried it but it had an error. But it looks like you've done it a different way so i'll try your method.

0

Also, what is the difference 'in pairs' and 'in ipairs'

0

it errors: bad argument #1 to 'ipairs' (table expected, got Object)

0

I fixed it. I added ":GetPlayers()" at the end of line 04 after the "Players" part

0

'ipairs' uses the order in the index for the index variable, while 'pairs' uses the key for the index variable. Sorry about that typo on line 4. Guess I overlooked it.

0

Its fine. You've helped me out alot. btw whilst we're here. I've encountered another problem. I am using local userID = game.Players:GetUserIdFromNameAsync(TopPlayer) then converting that userid to change the appearance of an npc. However that id is not the id used on my profile. Ik this coz my profile id on roblox is "22984342" and it is coming up with "73502"

0

You don't need to use the function. This would work: local userID = TopPlayer.UserId

0

oh right. Would that get me the 22984342 id?

0

It should if you have the most money.

0

Yup, Sweet thankyou soo much. Ill know that for future now as well. BRILLIANT! :D

0

Your welcome. Good luck on your game!

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