Trying to check the hightest value under a player, at the end of a round

  1. What do you want to achieve? I want that at the end of the round the player with most points wins (there is an intvalue with the points of the player under each player)

  2. What is the issue? With this script basically it prints me a random player, not the player with the hightest value. Then i need to give coins to the winner and i have to do a gui with the top 3 winners (with the user’s thumbails…).

All answers are welcome : D

local DataModel = game
local Players = DataModel:GetService("Players")

local function RankPlayers()
	local RankedPlayers = {}
	for _, Player in ipairs(Players:GetPlayers()) do
		local Stat = Player:FindFirstChild("SpainPoints") --Change to name of stat.
		if Stat then
			table.insert(RankedPlayers, {Player, Stat.Value})
		end
	end

	table.sort(RankedPlayers, function(Left, Right)
		if Left[2] < Right[2] then
			return Right
		end
	end)

	for _, PlayerArray in ipairs(RankedPlayers) do
		local Player = PlayerArray[1]
		local StatValue = PlayerArray[2]
	--	print(Player.Name, StatValue)
		local rankedlocalplayer = RankedPlayers[1]
		for i,v in pairs(RankedPlayers) do
			local player = v[1]
			local amount = v[2]
			if RankedPlayers[1] then
				print("The 1st is "..RankedPlayers[1][1].Name.." with "..RankedPlayers[1][2].." Points")
			elseif RankedPlayers[2] then
				print("The 2nd is "..RankedPlayers[2][1].Name.." with "..RankedPlayers[2][2].." Points")

			
			end
		end
	end

	return RankedPlayers
end

while wait(1) do
	RankPlayers()
end

You should never create new topics when it’s already solved.

1 Like

yes i know, i wrote this post 2 days ago, but i realized later that no one can understand with this post so i do another one… NVM this post

1 Like