How to get Value name after being sorted in Table

With this script I am trying to sort the Stats of each value. From there I want to get the highest value and make a TextBox in my game say “Most Sold Item - Paper(Or whatever the highest Value is)”.

This script allows the text to display the value of the highest Stat.

So I was wondering how to get it to display the name?

local ValuesStore = game.Players.LocalPlayer.Values
local Stats = {
	ValuesStore.PaperStats.Value,
	ValuesStore.PaperClipsStats.Value,
	ValuesStore.PensStats.Value,
	ValuesStore.MarkersStats.Value,
	ValuesStore.ChocolatesStats.Value,
	ValuesStore.GummiesStats.Value,
	ValuesStore.PopsStats.Value,
	ValuesStore.SourCandyStats.Value,
	ValuesStore.SodaStats.Value,
	ValuesStore.EnergyDrinkStats.Value,
	ValuesStore.CoffeeStats.Value,
	ValuesStore.MilkStats.Value}



table.sort(Stats)
print(Stats)

local Value = Stats[12]
local Text = script.Parent

Text.Text = "Most Sold Item - "..Value

Thank You

local Value = Stats[#Stats] I think this solved the problem.

1 Like

This still displays the stat value. Not the Name of the name of the stat.

Like it shows 14590.

1 Like

Reformat the array:

local Stats = {
	{Name = "PaperStats", Value = ValuesStore.PaperStats.Value},
	{Name = "PaperClips", Value = ValuesStore.PaperClipsStats.Value},
	...
}

table.sort(Stats, function(a, b)
    return a.Value > b.Value
end)

local highestStat = Stats[1]
local name = highestState.Name
local value = highestStat.Value
2 Likes
local Values = game.Players.LocalPlayer.Values

local Names = {"Paper", "PaperClips", "Pens", "Markers", "Chocolates", "Gummies", 
"Pops", "SourCandy", "Soda", "EnergyDrink", "Coffee", "Milk"}
local Stats = {}
for _, name in pairs(Names) do table.insert(Stats, Values[name.."Stats"]) end
--alternative if you want to pick all the stats under Values:
--local Stats = Values:GetChildren()

--sort in descending order according to value
table.sort(Stats, function(a, b)
	return a.Value > b.Value
end)

local Text = script.Parent
--the first element is the element with highest value/sales
Text.Text = "Most Solt Item - "..Stats[1].Name

Basically to fix the problem you’re facing you have to store the value instances instead of the value property in Stats and just sort according to the value property as shown above.

1 Like

Thank You this worked liked a charm.

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.