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

How to save another value in Data Storage?

Asked by
soutpansa 120
7 years ago

Inside of my data storage script, I want to add more values. For example, there is a value called "Cash" inside of a value called Levelss. I'd like to add another value, the same exact way that Cash is added, but every time I do, something went wrong. I'm still very new to Data Storage. Any ideas?

local DSService = game:GetService('DataStoreService'):GetDataStore('DataStoreName')
game.Players.PlayerAdded:connect(function(plr)

    local uniquekey = 'id-'..plr.userId
    local Levelss = Instance.new('IntValue', plr)
    local savevalue =  Instance.new('IntValue')
    Levelss.Name = 'Levelss'
    savevalue.Parent = Levelss
    savevalue.Name = 'Cash'

    -- GetAsync
    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.Levelss.Cash.Value}
DSService:SetAsync(uniquekey, Savetable)    


end)
0
What value are you trying to add and what's the error? Goulstem 8144 — 7y

1 answer

Log in to vote
0
Answered by 7 years ago
Edited 7 years ago

Didn't test it but it should work.

local DSService = game:GetService('DataStoreService'):GetDataStore('Stats')
ID = 0 --If you change this then it will reset everyones stats
SaveTime = 60 --Shouldn't be less then 60
leaderboardname = "Levelss" --The name of the leader board

StatList = {Cash = 0, StartingMoney = 5}
--StartingMoney would give new players the stat StartingMoney with the value 5
--Put any of your values here
--Values are loaded when the player joins and saved when they exit
--Values are also saved every SaveTime seconds

----- You shouldn't need to change anything below this line -----

function CreateValue(Name, Parent, Value) --If value is set to a number then new players will start with that amount.
    Value = Value or 0
    local savevalue =  Instance.new('IntValue', Parent)
    savevalue.Name = Name
    savevalue.Value = Value
    return savevalue
end

function GetValue(uniquekey)
    -- GetAsync
    local GetSaved = DSService:GetAsync(uniquekey)
    if GetSaved then
        return GetSaved
    end
end

game.Players.PlayerAdded:connect(function(plr)
    local uniquekey = ID..plr.userId
    local Levelss = Instance.new('IntValue', plr)
    Levelss.Name = leaderboardname
    for i,v in pairs(StatList) do
        local Stat = GetValue(i..uniquekey)
        if Stat then
            CreateValue(i, Levelss, Stat) --Old player load their value
        else
            CreateValue(i, Levelss, v) --New player load the default value
        end
    end
end)

game.Players.PlayerRemoving:connect(function(plr)
    local uniquekey = ID..plr.userId
    if plr:FindFirstChild(leaderboardname) then
        for i,v in pairs(StatList) do
            if plr[leaderboardname]:FindFirstChild(i) then
                DSService:SetAsync(i..uniquekey, plr[leaderboardname][i].Value)
            end
        end
    end
end)

while wait(SaveTime) do
    P = game:GetService("Players"):GetChildren()
    for o=1, #P do
        if P[o] then
            local uniquekey = ID..P[o].userId
            if P[o]:FindFirstChild(leaderboardname) then
                for i,v in pairs(StatList) do
                    if P[o][leaderboardname]:FindFirstChild(i) then
                        DSService:SetAsync(i..uniquekey, P[o][leaderboardname][i].Value)
                    end
                end
            end
        end
    end
end
0
Your script works but the stats will not load in with the value they are set to, startingmoney is still 0 soutpansa 120 — 7y
0
Missed a line, fixed it shadownetwork 233 — 7y
0
It worked, thanks. soutpansa 120 — 7y
0
Your welcome shadownetwork 233 — 7y
Ad

Answer this question