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
*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.