Leaderstats delay when loading in

My leaderstats take around 15 seconds to be shown to the player, despite the leaderstats folder being present. If second image doesn’t load, just click to see it.

You can see below an example of this.
image

This is what it should look like immediately:

Code:

local Players = game:GetService("Players")
local RunService = game:GetService("RunService")
local DatastoreService = game:GetService("DataStoreService")
local Data = DatastoreService:GetDataStore("NewDS")

function PlayerAdded(player)
	local leaderstats = Instance.new("Folder")
	leaderstats.Name = "leaderstats"
	leaderstats.Parent = player
	
	local Coins = Instance.new("IntValue")
	Coins.Value = 0
	Coins.Name = "Coins"
	Coins.Parent = leaderstats
	
	local Wins = Instance.new("IntValue")
	Wins.Value = 0
	Wins.Name = "Wins" 
	Wins.Parent = leaderstats
	
	local TimePlayed = Instance.new("IntValue")
	TimePlayed.Value = 0
	TimePlayed.Name = "TimePlayed"
	TimePlayed.Parent = leaderstats

	local success, playerData = pcall(function()
		return Data:GetAsync("k11111"..player.UserId)
	end)

	if success then
		print("Data loaded: " .. player.Name)

		if playerData then
			Coins.Value = playerData.Coins
			Wins.Value = playerData.Wins
			TimePlayed.Value = playerData.TimePlayed
		end
	else
		warn("Couldn't load data: " .. player.Name)
		player:Kick("Couldn't load your data, rejoin")
	end
end

function PlayerLeaving(player)
	local leaderstats = player:WaitForChild("leaderstats")
	local playerCoins = leaderstats.Coins.Value
	local playerWins = leaderstats.Wins.Value
	local playerTimePlayed = leaderstats.TimePlayed.Value
	
	local data = {
		Coins = playerCoins,
		Wins = playerWins,
		TimePlayed = playerTimePlayed
	}
	
	local success, errorMsg = pcall(function()
		Data:SetAsync("k11111"..player.UserId, data)
	end)

	if success then
		print("Data saved: " .. player.Name)
	else
		warn("Can't save: " .. player.Name)
	end
end

function ServerShutdown()
	if RunService:IsStudio() then
		return
	end


	for _, player in ipairs(Players:GetPlayers()) do
		task.spawn(function()
			PlayerLeaving(player)
		end)
	end
end


game:BindToClose(ServerShutdown)
Players.PlayerAdded:Connect(PlayerAdded)
Players.PlayerRemoving:Connect(PlayerLeaving)

The leaderstats menu is loaded as a part of the CoreGui and is referred to as “PlayerList” in the CoreGuiType documentation so the issue may be with how that is being loaded. I’m thinking you could either create your own custom players list or experiment with the PlayersList CoreGui. Idk how exactly it works though

2 Likes

Turns out, one of my other scripts was disabling the player list in CoreGui. Wouldn’t have figured that out without this.

1 Like

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.