Leaderstats load very slow

Whenever I load into my game the leaderstats don’t load until 15 seconds of being in the game which ruins lots of features in the game. I’ve tried a lot of thing but they don’t seem to work. I’m not sure if something is messed up in my leaderstats script. Here it is:

local PlayerStatsDS = game:GetService("DataStoreService"):GetDataStore("Player_StatsLOL")

game.Players.PlayerAdded:Connect(function(NP)
	
	local Key = "PDS-".. NP.UserId
	
	local GetSave = PlayerStatsDS:GetAsync(Key)
	
	local PSF = Instance.new("Folder", NP)
	PSF.Name = "leaderstats"
	
	local StatsFolder = script.Stats
	
	for _, S in pairs(StatsFolder:GetChildren()) do
		local NS = Instance.new(S.ClassName, PSF)
		NS.Name = S.Name
		NS.Value = S.Value
	end
	
	local timeplayed = PSF:WaitForChild("Time Played")
	
	spawn(function()
		while true do
			wait(1)
			timeplayed.Value = timeplayed.Value + 1
		end
	end)
	
	if GetSave then
		for n, Stat in pairs(PSF:GetChildren()) do
			Stat.Value = GetSave[n]
		end
	else
		local STS = {}
		for _, Stat in pairs(StatsFolder:GetChildren()) do
			table.insert(STS, Stat.Value)
		end
		PlayerStatsDS:SetAsync(Key, STS)
	end
end)

game.Players.PlayerRemoving:connect(function(OP)
	
	local Key = "PDS-".. OP.UserId
	
	local StatsFolder = OP.leaderstats
	
	local STS = {}
	for _, Stat in pairs(StatsFolder:GetChildren()) do
		table.insert(STS, Stat.Value)
	end
	PlayerStatsDS:SetAsync(Key, STS)
end)

Any help would be amazing! :slight_smile:

2 Likes

It is slow caused by the datastore

1 Like

is there a possible way to make it quicker?

1 Like
local players = game:GetService("Players")
local DataStoreService = game:GetService("DataStoreService")
local timePlayedStore = datastores:GetDataStore("DataStore")

local function onPlayerAdded(player)
	local leaderstats = Instance.new("Folder")
    leaderstats.Parent = player
	leaderstats.Name = "leaderstats"
	
	local seconds = Instance.new("IntValue")
    seconds.Parent = leaderstats
	seconds.Name = "Seconds"
	
	local success, result = pcall(function()
		return timePlayedStore:GetAsync("Seconds_"..player.UserId)
	end)
	
	if success then
		if result then
			seconds.Value = result
		end
	else
		warn(result)
	end
	
	task.spawn(function()
		while task.wait(1) do
			seconds.Value += 1
		end
	end)
end

local function onPlayerRemoving(player)
	local seconds = player.leaderstats.Seconds
	local success, result = pcall(function()
		timePlayedStore:UpdateAsync("Seconds_"..player.UserId, function(old)
			return seconds.Value
		end)
	end)
	
	if success then
		if result then
			print(result)
		end
	else
		warn(result)
	end
end

players.PlayerAdded:Connect(onPlayerAdded)
players.PlayerRemoving:Connect(onPlayerRemoving)

Make the leaderstats before loading datastore might help

1 Like

This didn’t work at all sadly. Didn’t add leaderstats to the player.

I tried that previously, but it just made the wait time longer.

Make 2 separate scripts, that’s what I do.

A separate leaderstats and datastore one?

Indeed, I always made a script for leaderstat and another for datastore. Loading is normal for me.

Hmm alright ill give it a go. I also noticed that the leaderstats bar on the top right never appears even when the data is loaded.

Fixed it. This should work.

local players = game:GetService("Players")
local DataStoreService = game:GetService("DataStoreService")
local timePlayedStore = datastores:GetDataStore("DataStore")

local function onPlayerAdded(player)
	local leaderstats = Instance.new("Folder")
    leaderstats.Parent = player
	leaderstats.Name = "leaderstats"
	
	local seconds = Instance.new("IntValue")
    seconds.Parent = leaderstats
	seconds.Name = "Seconds"
	
	local success, result = pcall(function()
		return timePlayedStore:GetAsync("Seconds_"..player.UserId)
	end)
	
	if success then
		if result then
			seconds.Value = result
		end
	else
		warn(result)
	end
	
	task.spawn(function()
		while task.wait(1) do
			seconds.Value += 1
		end
	end)
end

local function onPlayerRemoving(player)
	local seconds = player.leaderstats.Seconds
	local success, result = pcall(function()
		timePlayedStore:UpdateAsync("Seconds_"..player.UserId, function(old)
			return seconds.Value
		end)
	end)
	
	if success then
		if result then
			print(result)
		end
	else
		warn(result)
	end
end

players.PlayerAdded:Connect(onPlayerAdded)
players.PlayerRemoving:Connect(onPlayerRemoving)

This did indeed work. But the only issue is that the leaderstats board doesn’t appear after 15 seconds.

1 Like

What part of your Original Script makes your leaderstats board appear after 15 seconds?

Nevermind, I got it fixed! Thanks for the help!

1 Like