Trying To Sort Players In A Table Based On The Amount Of Jewels They Have

There’s no reason why this would stop it from counting the jewels. How have you implemented the updating of the gem value? Is it a local script or a server script?

Similarly, is the script you posted above in a local or a server script?

Some changes for some extra checks:

local ReplicatedStorage = game:GetService("ReplicatedStorage")

local DEBUG = true --> Turn it off to stop printing the gem data
local plrs = { }
local plr_limit = 2
local func_started = false

local function SortByGemsDescending(plrs)
	local playerRankingsDictionary = { }
	for i, player in next, plrs do
		player = game.Players:FindFirstChild(player)
		if player and player:IsA 'Player' and player:FindFirstChild 'jewels' then
			playerRankingsDictionary[#playerRankingsDictionary + 1] = {player = player; jewels = player.jewels.Value}
		end
	end 
	
	table.sort(playerRankingsDictionary, function (a, b)
		return a.jewels > b.jewels
	end)
	return playerRankingsDictionary
end

local function printGemList(plrs)
	local function ordinalNumber(n)
		local ordinal, digit = {"st", "nd", "rd", "th", "th"}, string.sub(n, -1)
		if tonumber(digit) > 0 and tonumber(digit) <= 3 and string.sub(n, -2) ~= 1 and string.sub(n, -2) ~= 2 and string.sub(n, -2) ~= 3 then
			return n .. ordinal[tonumber(digit)]
		else
			return n .. "th"
		end
	end
	
	local orderedList = SortByGemsDescending(plrs)
	for i, packet in next, orderedList do
		if packet.player and packet.player.Parent then
			print(
				('%s is %d%s with %d jewels!'):format(packet.player.Name, i, ordinalNumber(i), packet.jewels)
			)
		end
	end
end

local function game_ended()
	--> Do stuff with the ranked list here
	local rankedByJewelList = SortByGemsDescending(plrs)
	
	--> Print the gem list for debug purposes
	if DEBUG then
		printGemList(plrs)
	end
	
	--> Reset our func state
	func_started = false
end

local function start_script()
	--> If we've already started, don't start it again
	if func_started then
		return
	end
	local timeLeft = 20
	for i = 1, timeLeft do
		for i, player in pairs(plrs) do
			local plr = game.Players:FindFirstChild(player)
			timeLeft = timeLeft - 1
			plr.gameTime.Value = plr.gameTime.Value - 1
			if timeLeft == 1 then
				print("fire game ended")
				game_ended()
				break
			end
		end
		wait(1)
	end
end

local function check_plr(plr)
	local found_plr = false
	
	for _,v in pairs(plrs) do
		if v == plr then
			found_plr = true
			break
		end
	end
	
	return found_plr
end

	
script.Parent.Touched:Connect(function (p)
  if #plrs < plr_limit and p and p.Parent and p.Parent:FindFirstChildOfClass("Humanoid") and not check_plr(p.Parent.Name) then
	--> Let's make sure all the pieces are in the right place, if so, we can add them to our list
	if p.Parent:FindFirstChild 'Head' then
		if p.Parent.Head:FindFirstChild 'BillboardGui' then
			table.insert(plrs, p.Parent.Name)
			p.Parent.Head.BillboardGui.Enabled = true
		end
	end
	--> Start it now if we have enough players
	if #plrs >= plr_limit and not func_started then
		func_started = true
		start_script()
	end
  elseif #plrs >= plr_limit and not func_started then
	func_started = true
	start_script()
  end
end)
1 Like