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