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

How To Find Highest Value Then Add to table?

Asked by
Vezious 310 Moderation Voter
8 years ago

I Need Help On creating A script, I'm not sure how'd you do this. There is a Folder. Here is the Format

Script.Players(Folder).Player(NumValue)

Player Is Set Up as Name = Player Name And Value = Value

So What I am trying to say is something like this,

for i=1,HowManyDoIFind.Value  do
Find Highest Value
Return The Value And The Name
Insert Name And Values In Order On Table Named "Scores"
end

And Whenever I Need The 1st Winner Or 5th Winner, I just Access The Table. Problem Is I Don't Know How to make the script.

0
Show us your attempt Perci1 4988 — 8y
2
He made an attempt at PsuedoCode if that counts .-. DigitalVeer 1473 — 8y

1 answer

Log in to vote
2
Answered by
BlueTaslem 18071 Moderation Voter Administrator Community Moderator Super Administrator
8 years ago

The simplest way to accomplish this would be sort the list of Player NumberValues.

To sort, you need a list:

local scores = script.Players:GetChildren()

and a way to compare two things (a function). This function is supposed to return true if first should be ranked before second:

function compare(first, second)
    return first.Value > second.Value
end

Now you just call table.sort using scores and compare:

table.sort(scores, compare)

and then scores[1] is the highest scoring player, up to scores[#scores]:

for i = 1, #scores do
    print(i .. "st", scores[i].Name, "scored", scores[i].Value, "points")
end
Ad

Answer this question