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

How to create a Data Store that saves all leaderstats instead of one?

Asked by 7 years ago

Currently this is the script I have

local DSService = game:GetService('DataStoreService'):GetDataStore('Currency')
game.Players.PlayerAdded:connect(function(plr)
    --[[ Stat1 ]]--
    local uniquekey = 'id-'..plr.userId
    local leaderstats = Instance.new('IntValue', plr)
    local savevalue =  Instance.new('IntValue')
    leaderstats.Name = 'leaderstats'
    savevalue.Parent = leaderstats
    savevalue.Name = 'Tokens' -- Stat Name


    local GetSaved = DSService:GetAsync(uniquekey)
    if GetSaved then
        savevalue.Value = GetSaved[1]
    else
        local NumbersForSaving = {savevalue.Value}
        DSService:SetAsync(uniquekey, NumbersForSaving)
    end
end)

game.Players.PlayerRemoving:connect(function(plr)
local uniquekey = 'id-'..plr.userId
local Savetable = {plr.leaderstats.Tokens.Value} -- Stat Name
DSService:SetAsync(uniquekey, Savetable)    


end)

I know I can just copy and paste and edit the stat names but I have a ton of stats and It would be very time consuming. Please help me!

1 answer

Log in to vote
0
Answered by 7 years ago

Use a table, datastores can save tables. So for example:

local datastore = game:GetService("DataStoreService"):GetDataStore("TestStore")
local myTable = {
    "string",
    1,
    true,
    workspace.Part
}

local mykey = "key"

datastore:SetAsync(key, myTable)

No problem.

0
Thank you Michael_TheCreator 166 — 7y
0
You're welcome :P Operation_Meme 890 — 7y
Ad

Answer this question