Scripting Helpers is winding down operations and is now read-only. More info→
Ad
Log in to vote
0

Why don't my leaderstats or any stats save?

Asked by 2 years ago

For a bit more detail, everything is enabled in this game. This effects both in and out of studio. I've tried the common form of saving leaderstats. This is the script I am using for my leaderstats:

local ds = game:GetService("DataStoreService"):GetDataStore("LeaderStore")

nc0 = "Z-Bucks"
nc1 = "Waves"

sc0 = 0
sc1 = 0

game.Players.PlayerAdded:Connect(function(player)
    wait(0.1)
    local savedLevel = ds:GetAsync(player.UserId)

    local coins = player.leaderstats["Z-Bucks"]

    local coins1 = player.leaderstats.Waves

    if savedLevel then
        coins.Value = savedLevel[1]
        coins1.Value = savedLevel[2]
    else
        coins.Value = sc0
        coins1.Value = sc1

        ds:SetAsync(player.UserId,{coins.Value, coins1.Value})
    end
end)

game.Players.PlayerRemoving:Connect(function(player)
    local stats = player:FindFirstChild("leaderstats")
    if not stats then return end
    local vals = {stats:FindFirstChild(nc0),stats:FindFirstChild(nc1)}
    if vals and vals[1] and vals[2] then
        ds:SetAsync(player.UserId,{vals[1].Value, vals[2].Value})
    end
end)

If you could provide a script that works that will be appreciated!

0
You're not initializing the leaderstats folder in the first place real_cana 0 — 2y
0
That and please you should use a pcall for Datastores and everyting Async because Errors are a lot more likely there xXMadonXx 190 — 2y
0
I already have a leaderstats. But how would I use pcall and async? I am not that good at coding. Minecrafttake2 0 — 2y

1 answer

Log in to vote
0
Answered by 2 years ago

Never mind, I found a script that saves it. Here is the script if anyone needs it:

local DataStore = game:GetService("DataStoreService"):GetDataStore("PlayerStats")

game.Players.PlayerAdded:Connect(function(p)

local leaderstats = Instance.new("Folder")
leaderstats.Name = "leaderstats"
leaderstats.Parent = p

local Cash = Instance.new("IntValue")
Cash.Name = "Z-Bucks"
Cash.Parent = leaderstats

local Survived = Instance.new("IntValue")
Survived.Name = "Waves"
Survived.Parent = leaderstats

wait(2)

 local key = "id_"..p.UserId
 local Cashs = p.leaderstats.Waves
 local Surviveds = p.leaderstats["Z-Bucks"]
 local Cooldowns = p.Cooldown
 local SoundIDs = p.IdOfSound
 local soundPlayss = p.soundPlays
 local FPSs = p.showFPS

local Saved = DataStore:GetAsync(key)
if Saved then
    Cash.Value = Saved[1]
    Survived.Value = Saved[2]
else
    local SaveNum = {Surviveds.Value, Cashs.Value, Cooldowns.Value, SoundIDs.Value, soundPlayss.Value, FPSs.Value}
    DataStore:GetAsync(key, SaveNum)
end

end)

game.Players.PlayerRemoving:Connect(function(p) DataStore:SetAsync("id_"..p.UserId, {p.leaderstats.Waves.Value, p.leaderstats["Z-Bucks"].Value}) end)

You can add more values in case you needed more to be saved.

Ad

Answer this question