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)
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)
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)