Problem with checking the hightest value

  1. What do i want to achieve? I want that at the end of a round there is a frame with the name of the player with the hightest round points (an intvalue under each player). Basically i found this script and i modificated it, but the problem is that every 5 seconds (bc for now i put while wait(5) do) the textlabel with the name of the player of the hightest value change with random players (watch the video for understand more)

robloxapp-20221016-1359100.wmv (4.6 MB)

This is my script

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 pairs(RankedPlayers) do
		local Player = PlayerArray[1]
		local StatValue = PlayerArray[2]
		print(Player.Name, StatValue)
		game.ReplicatedStorage.SPIN.Value = "The winner is ".. Player.Name.." with "..StatValue.." Points"
		if Player then
			local userid = Player.UserId
			local thumtype = Enum.ThumbnailType.HeadShot
			local thumSize = Enum.ThumbnailSize.Size420x420
			local content = game.Players:GetUserThumbnailAsync(userid, thumtype, thumSize)
			game.ReplicatedStorage.SPINimage.Value = content
			
		   	game.ReplicatedStorage.GoSpainGUIvalue:FireAllClients()
			
			local cloned = game.ReplicatedStorage.PointsGiver:Clone()
			cloned.TextLabel.Text = "GG! You finished first and won 30 points!"
	cloned.Parent = Player.PlayerGui
			cloned.TextLabel:TweenPosition(UDim2.new(0.252, 0,0.834, 0), Enum.EasingDirection.InOut, Enum.EasingStyle.Back, 1, false)
			Player.leaderstats.Points.Value = Player.leaderstats.Points.Value + 30
			wait(4)
			cloned.TextLabel:TweenPosition(UDim2.new(0.252, 0,0.99, 0), Enum.EasingDirection.InOut, Enum.EasingStyle.Back, 1, false)
			wait(1)
			cloned:Destroy()
			---
					
				      
				
		end
	end

	return RankedPlayers
end	

while wait(5) do
 RankPlayers()
	end

sry if you don’t understand,
all answers are welcome
i took the @Forummer script

1 Like

Im pretty sure thats not how table.sort() works, in your case, its supposed to look like this

table.sort(RankedPlayers, function(a,b)
   return a[2] < b[2]
end)
1 Like

Ty for the answer, but it is not working, same error

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(a, b)
		return a[2] < b[2] 	
	end)

	for _, PlayerArray in pairs(RankedPlayers) do
		local Player = PlayerArray[1]
		local StatValue = PlayerArray[2]
		print(Player.Name, StatValue)
		game.ReplicatedStorage.SPIN.Value = "The winner is ".. Player.Name.." with "..StatValue.." Points"
		if Player then
			local userid = Player.UserId
			local thumtype = Enum.ThumbnailType.HeadShot
			local thumSize = Enum.ThumbnailSize.Size420x420
			local content = game.Players:GetUserThumbnailAsync(userid, thumtype, thumSize)
			game.ReplicatedStorage.SPINimage.Value = content
			
		   	game.ReplicatedStorage.GoSpainGUIvalue:FireAllClients()
			
			local cloned = game.ReplicatedStorage.PointsGiver:Clone()
			cloned.TextLabel.Text = "GG! You finished first and won 30 points!"
	cloned.Parent = Player.PlayerGui
			cloned.TextLabel:TweenPosition(UDim2.new(0.252, 0,0.834, 0), Enum.EasingDirection.InOut, Enum.EasingStyle.Back, 1, false)
			Player.leaderstats.Points.Value = Player.leaderstats.Points.Value + 30
			wait(4)
			cloned.TextLabel:TweenPosition(UDim2.new(0.252, 0,0.99, 0), Enum.EasingDirection.InOut, Enum.EasingStyle.Back, 1, false)
			wait(1)
			cloned:Destroy()
			---
					
				      
				
		end
	end

	return RankedPlayers
end	

while wait(5) do
 RankPlayers()
	end

Whats the error because I cant test it

Basically every 5 seconds the player in the textlabel change. For example if player1 has 5 points and player2 has 0 then the textlabel show
every 5 seconds a random different player, player1 and player2 every 5 seconds (bc of the while wwait(5) do). I want that it shows ONLY the player with the hightest value *

Well, that makes things less complicated.

function GetPlayerWithHighestScore() -- Name is unreasonably long, cant think of a better one
   local plrs = {}

   for _, p in game.Players:GetPlayers() do
      local l = p:FindFirstChild("leaderstats")
      if l then
         local stat = l:WaitForChild("statname")
         plrs[p] = stat
      end
   end

   local best = {
      plr = nil;
      val = 0:
   }

   for p, val in plrs do
      if val > best.val then
         best.plr = p
         best.val = val
      end
   end

   return best.plr,best.val
end

local bestplr,val = GetPlayerWithHighestScore()

If this doesn’t work, then tell me the error because I wrote this on mobile.

1 Like

Tysm for the script

It prints me

nil 0

image

Here is the script

function GetPlayerWithHighestScore() 
local plrs = {}

for _, p in game.Players:GetPlayers() do
		local stat = p:WaitForChild("SpainPoints")
		if stat then
		plrs[p] = stat
	end
end

local best = {
	plr = nil;
	val = 0
}

for p, val in plrs do
	if val > best.val then
		best.plr = p
		best.val = val
	end
end

return best.plr,best.val
end

local bestplr,val = GetPlayerWithHighestScore()

while wait(2) do
	print(bestplr, val)
	end```

Try printing val before comparing if it is greater than.

for p, val in plrs do
    print(val)
	if val > best.val then
		best.plr = p
		best.val = val
	end
end
1 Like
function GetPlayerWithHighestScore() 
local plrs = {}

	for _, p in ipairs(game.Players:GetPlayers() ) do
		local stat = p:FindFirstChild("SpainPoints")
		if stat then
		plrs[p] = stat
	end
end

local best = {
	plr = nil;
	val = 0
}

	for p, val in plrs do
		print(val)
		if val > best.val then
			best.plr = p
			best.val = val
		end
		end
		

return best.plr,best.val
end

local bestplr,val = GetPlayerWithHighestScore()

It is not printing with this script

Ok, that must mean the plrs table is empty, can you try printing the plr too?

for _, p in ipairs(game.Players:GetPlayers() ) do
    print(p)
	local stat = p:FindFirstChild("SpainPoints")
	if stat then
		plrs[p] = stat
	end
end

Also, what is ipars() for? Why can’t you just loop through the Players table?

1 Like

I connected the function to playeradded and it is printing my name, if i don’t connect the function it doesn’t print

You can’t use a string for call instance into player

1 Like

Which part are you talking about?

Actually, what do you mean by

1 Like

Ok, I rewrote the script to use UserIDs instead of Player Objects

function GetPlayerWithHighestScore() -- Name is unreasonably long, cant think of a better one
   local plrs = {}

   for _, p in game.Players:GetPlayers() do
      local l = p:FindFirstChild("leaderstats")
      if l then
         local stat = l:WaitForChild("statname")
         plrs[p.UserId] = stat
      end
   end

   local best = {
      id = 1;
      val = "If it prints this, then that means it didnt work.";
   }

   for id, val in plrs do
      if val > best.val then
         best.id = id
         best.val = val
      end
   end

   return game.Players:GetPlayerByUserId(best.id),best.val
end

local bestplr,val = GetPlayerWithHighestScore()
1 Like

image

function GetPlayerWithHighestScore() – Name is unreasonably long, cant think of a better one
local plrs = {}

for _, p in game.Players:GetPlayers() do
	local stat = p:WaitForChild("SpainPoints")
	if stat then
		plrs[p.UserId] = stat
	end
end

local best = {
	id = 0;
	val = "If it prints this, then that means it didnt work.";
}

for id, val in plrs do
	if val > best.val then
		best.id = id
		best.val = val
	end
end

return game.Players:GetPlayerByUserId(best.id),best.val

end

local bestplr,val = GetPlayerWithHighestScore()

print(bestplr, val)

Yeah that didn’t work either. Can you try printing the plrs table?

print(table.unpack(plrs))
1 Like

Wait, I think I know why now, maybe its because the players haven’t loaded in yet? So connect the function to game.Players.PlayerAdded

1 Like

image
it prints me this strange thing

Ok yes, I’m sure that the players havent loaded in. Try putting it in a while loop or playeradded

1 Like

i guess that i need to use toastring or something similar