Problem with checking the hightest value

Actually no, just put it in a while loop

while task.wait() do
   local bestplr,val = GetPlayerWithHighestScore()
   print(bestplr,val)
end

Edit : Wait, I didnt see the error. Let me check the script again.

1 Like

image

You are sure you are pressing Play and not Run right?

1 Like

yes, i am sure, i never use run

Then sorry, I dont really know how to help you anymore…

1 Like

You were looping through all players when cloning the Gui showing their avatar and their points, I fixed that problem and cleaned up your code a little.

local players = game:GetService("Players")
local replicatedStorage = game:GetService("ReplicatedStorage")
local runService = game:GetService("RunService")

local function rankPlayers()
	local rankedplayers = {}

	for _, v in ipairs(players:Getplayers()) do
		local stat = v:FindFirstChild("SpainPoints") --Change to name of stat.
		if not stat then continue end
		table.insert(rankedplayers, {v, stat.Value})
	end

	table.sort(rankedplayers, function(a, b)
		return b[2] < a[2]
	end)

	local player = rankedplayers[1][1]
	local statValue = rankedplayers[1][2]

	print("The winner is: "..player.Name..", and has: "..statValue.." points.")

	replicatedStorage.SPIN.Value = "The winner is ".. player.Name.." with "..statValue.." Points"

	if player then
		local userId = player.UserId
		local thumbnailType = Enum.ThumbnailType.HeadShot
		local thumbnailSize = Enum.ThumbnailSize.Size420x420
		local content = players:GetUserThumbnailAsync(userId, thumbnailType, thumbnailSize)

		replicatedStorage.SPINimage.Value = content
		replicatedStorage.GoSpainGUIvalue:FireAllClients()
			
		local cloned = 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
		task.wait(4)

		cloned.TextLabel:TweenPosition(UDim2.new(0.252, 0,0.99, 0), Enum.EasingDirection.InOut, Enum.EasingStyle.Back, 1, false)

		task.wait(1)

		cloned:Destroy()
	end

	return rankedplayers
end

local lastTick = tick()

runService.HeartBeat:Connect(function()
    if tick() - lastTick < 5 then return end
	rankPlayers()

    lastTick = tick()
end)
1 Like

Tysm i connected the function to a bindable event and it works perfectly

1 Like