Global leaderboard doesn't work

Hello I’m trying to set the data to the blockdistance of the Player from the Part. But it does not work for some reason. I’ve added a IntValue to the Player that shows the distance to the Block but the data won’t get the Value of it and won’t show the Studs in the leaderboard.

Leaderboard script:

local DataStoreService = game:GetService("DataStoreService")
local StudsDatastore = DataStoreService:GetOrderedDataStore("StudsWalk")

local RefreshRate = 60

local function RefreshLeaderboard()

	for i, Player in pairs(game.Players:GetPlayers()) do
		StudsDatastore:SetAsync(Player.UserId, Player.StudsFolder.Studs.Value) -- Does not work
	end
	
	local Success, Error = pcall(function()

		local Data = StudsDatastore:GetSortedAsync(false, 10) 
		local StudsPage = Data:GetCurrentPage()

		for Rank, SavedData in ipairs(StudsPage) do 

			local Username = game.Players:GetNameFromUserIdAsync(tonumber(SavedData.key))
			local Studs = SavedData.value

			if Studs then

				local NewSample = script.Parent.Sample:Clone()

				NewSample.Visible = true
				NewSample.Parent = script.Parent
				NewSample.Name = Username

				NewSample.RankLable.Text = "#"..Rank
				NewSample.NameLable.Text = Username
				NewSample.CashLable.Text = Studs
			end
		end
	end)
end

if game.Players.PlayerAdded then
	RefreshLeaderboard()	
end

if game.Players.PlayerRemoving then
	RefreshLeaderboard()
end

while true do

	for i, Frame in pairs(script.Parent:GetChildren()) do
		if Frame.Name ~= "Sample" and Frame:IsA("Frame") then
			Frame:Destroy()
		end
	end

	RefreshLeaderboard()
	wait(RefreshRate)
end

Block distance script:

local player = game.Players.LocalPlayer

local runService = game:GetService("RunService")

local redPart = workspace.MeterBlock


runService.RenderStepped:Connect(function()
	player:WaitForChild("StudsFolder").Studs.Value = math.floor((redPart.Position - player.Character.Head.Position).Magnitude)
end)

Player-> StudsFolder-> Studs

Player-> StudsFolder-> Studs

1 Like