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

Leaderstat saving somehow broke over time?

Asked by
Gogulsky 129
2 years ago

I've been using a stat script in my game and it has been saving until one time when everyone started telling me that their stats didn't save. I don't know what the reason is. This is the script:

game.Players.PlayerAdded:Connect(function(plr)
    local DS = game:GetService("DataStoreService")

    local Stats = Instance.new("Folder", plr)
    Stats.Name = "Stats"    

    for i,v in pairs(script:GetChildren()) do    
        local DSnew = DS:GetDataStore(v.name)
        local newStat = Instance.new("NumberValue",Stats)
        newStat.Name = v.Name
        newStat.Value = DSnew:GetAsync(plr.userId.."-testdatastores") or v.value
    end
end)



game. Players.PlayerRemoving:Connect(function(plr)    
    for i,v in pairs(script:GetChildren()) do
        print("Accept!")
        local DS = game:GetService("DataStoreService")
        local DSnew = DS:GetDataStore(v.name)
        DSnew:SetAsync(plr.UserId.."-testdatastores", plr.Stats[v.name].Value)  
        print("Saved!")
    end
end)

It has a couple of NumberValues with the stat names but nothing happened when I added more, it just kind of started to break out of nowhere and I need help on understanding why this happened and how can I possibly fix this issue.

1 answer

Log in to vote
1
Answered by 2 years ago

First, you should NEVER parent a new instance in the parameters. Second, you should not create many DataStores, it will break your DataStore system and it may not work properly(it may give you some warnings). You should save those values in a table, it only uses one data store, and it's more effective when you want to get someone's money or whatever value you want. (if you want an example, please tell me)

0
thanks, already fixed this and made the script more complex with the help of your reply Gogulsky 129 — 2y
Ad

Answer this question