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

How do i compare leaderstats in a server to find the player with the highest stat?

Asked by 6 years ago

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

Log in to vote
0
Answered by
Dog2puppy 168
6 years ago
Edited 6 years ago

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

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. NoirPhoenix 148 — 6y
0
Also, what is the difference 'in pairs' and 'in ipairs' NoirPhoenix 148 — 6y
0
it errors: bad argument #1 to 'ipairs' (table expected, got Object) NoirPhoenix 148 — 6y
0
I fixed it. I added ":GetPlayers()" at the end of line 04 after the "Players" part NoirPhoenix 148 — 6y
View all comments (7 more)
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. Dog2puppy 168 — 6y
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" NoirPhoenix 148 — 6y
0
You don't need to use the function. This would work: local userID = TopPlayer.UserId Dog2puppy 168 — 6y
0
oh right. Would that get me the 22984342 id? NoirPhoenix 148 — 6y
0
It should if you have the most money. Dog2puppy 168 — 6y
0
Yup, Sweet thankyou soo much. Ill know that for future now as well. BRILLIANT! :D NoirPhoenix 148 — 6y
0
Your welcome. Good luck on your game! Dog2puppy 168 — 6y
Ad

Answer this question