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

Saving multiple values at once using DataStores?

Asked by
KordGamer 155
7 years ago

I've always had a problem with DataStores. I don't like using them, and I always come across an error. This time, I don't know really how to save multiple values at once.

Description of what I'm doing (context) I'm saving a player's 'resources' (amount of wood, rock, sticks, stones, metal etc). These are IntValues, and there are 16 of them. Every time a person joins the server, the server makes a folder in the player named simply "Resources" and will then put 16 IntValues in it (All named the name of the resource they are, for example "Metal"). I need to save and load these every time a person leaves/joins.

This is the whole datastore script:

local ds = game:GetService('DataStoreService'):GetDataStore("Resources")

game.Players.PlayerAdded:connect(function(plr)
    local key = tostring(plr.userId).."_resource"
    local data1 = ds:GetAsync(key[1])
    local data2 = ds:GetAsync(key[2])
    local data3 = ds:GetAsync(key[3])
    local data4 = ds:GetAsync(key[4])
    local data5 = ds:GetAsync(key[5])
    local data6 = ds:GetAsync(key[6])
    local data7 = ds:GetAsync(key[7])
    local data8 = ds:GetAsync(key[8])
    local data9 = ds:GetAsync(key[9])
    local data10 = ds:GetAsync(key[10])
    local data11 = ds:GetAsync(key[11])
    local data12 = ds:GetAsync(key[12])
    local data13 = ds:GetAsync(key[13])
    local data14 = ds:GetAsync(key[14])
    local data15 = ds:GetAsync(key[15])
    local data16 = ds:GetAsync(key[16])
    data1 = data1 or 0
    data2 = data2 or 0
    data3 = data3 or 0
    data4 = data4 or 0
    data5 = data5 or 0
    data6 = data6 or 0
    data7 = data7 or 0
    data8 = data8 or 0
    data9 = data9 or 0
    data10 = data10 or 0
    data11 = data11 or 0
    data12 = data12 or 0
    data13 = data13 or 0
    data14 = data14 or 0
    data15 = data15 or 0
    data15 = data16 or 0 
    local mstat1 = plr.Resources.Wood
    local mstat2 = plr.Resources.Rock
    local mstat3 = plr.Resources.Sticks
    local mstat4 = plr.Resources.Stones
    local mstat5 = plr.Resources.Metal
    local mstat6 = plr.Resources.Gold
    local mstat7 = plr.Resources.Steel
    local mstat8 = plr.Resources.Bronze
    local mstat9 = plr.Resources.Adurite
    local mstat10 = plr.Resources.Viridian
    local mstat11 = plr.Resources["Black Iron"]
    local mstat12 = plr.Resources.Bombastic
    local mstat13 = plr.Resources.Laser
    local mstat14 = plr.Resources.Gamma
    local mstat15 = plr.Resources.Tech
    local mstat16 = plr.Resources.Mythium
    mstat1.Value = data1
    mstat2.value = data2
    mstat3.Value = data3
    mstat4.value = data4
    mstat5.Value = data5
    mstat6.value = data6
    mstat7.Value = data7
    mstat8.value = data8
    mstat9.Value = data9
    mstat10.value = data10
    mstat11.Value = data11
    mstat12.value = data12
    mstat13.Value = data13
    mstat14.value = data14
    mstat15.Value = data15
    mstat16.value = data16

    print("All the keys has been loaded for "..plr.."!")
end)

game.Players.PlayerRemoving:connect(function(plr)
    local key = tostring(plr.userId).."_resource"
    local stat1 = plr.Resources.Wood
    local stat2 = plr.Resources.Rock
    local stat3 = plr.Resources.Sticks
    local stat4 = plr.Resources.Stones
    local stat5 = plr.Resources.Metal
    local stat6 = plr.Resources.Gold
    local stat7 = plr.Resources.Steel
    local stat8 = plr.Resources.Bronze
    local stat9 = plr.Resources.Adurite
    local stat10 = plr.Resources.Viridian
    local stat11 = plr.Resources["Black Iron"]
    local stat12 = plr.Resources.Bombastic
    local stat13 = plr.Resources.Laser
    local stat14 = plr.Resources.Gamma
    local stat15 = plr.Resources.Tech
    local stat16 = plr.Resources.Mythium
    local data = ds:GetAsync(key)
    if data ~= nil then
        print("hopefully we won't explode the servers ;]")
        ds:UpdateAsync(key[1],function(old) return stat1.Value end)
        ds:UpdateAsync(key[2],function(old) return stat2.Value end)
        ds:UpdateAsync(key[3],function(old) return stat3.Value end)
        ds:UpdateAsync(key[4],function(old) return stat4.Value end)
        ds:UpdateAsync(key[5],function(old) return stat5.Value end)
        ds:UpdateAsync(key[6],function(old) return stat6.Value end)
        ds:UpdateAsync(key[7],function(old) return stat7.Value end)
        ds:UpdateAsync(key[8],function(old) return stat8.Value end)
        ds:UpdateAsync(key[9],function(old) return stat9.Value end)
        ds:UpdateAsync(key[10],function(old) return stat10.Value end)
        ds:UpdateAsync(key[11],function(old) return stat11.Value end)
        ds:UpdateAsync(key[12],function(old) return stat12.Value end)
        ds:UpdateAsync(key[13],function(old) return stat13.Value end)
        ds:UpdateAsync(key[14],function(old) return stat14.Value end)
        ds:UpdateAsync(key[15],function(old) return stat15.Value end)
        ds:UpdateAsync(key[16],function(old) return stat16.Value end)
        print("All the keys has been saved!")
    else
        print("hopefully we won't explode the servers ;] [2]")
        ds:SetAsync(key[1],stat1.Value)
        ds:SetAsync(key[2],stat2.Value)
        ds:SetAsync(key[3],stat3.Value)
        ds:SetAsync(key[4],stat4.Value)
        ds:SetAsync(key[5],stat5.Value)
        ds:SetAsync(key[6],stat6.Value)
        ds:SetAsync(key[7],stat7.Value)
        ds:SetAsync(key[8],stat8.Value)
        ds:SetAsync(key[9],stat9.Value)
        ds:SetAsync(key[10],stat10.Value)
        ds:SetAsync(key[11],stat11.Value)
        ds:SetAsync(key[12],stat12.Value)
        ds:SetAsync(key[13],stat13.Value)
        ds:SetAsync(key[14],stat14.Value)
        ds:SetAsync(key[15],stat15.Value)
        ds:SetAsync(key[16],stat16.Value)
        print("All the keys have been saved! [2]")
    end
end)

And for context, here's the resources folder creation script:

game.Players.PlayerAdded:connect(function(player)
    local ResourceFolder = Instance.new("Folder", player)
    ResourceFolder.Name = "Resources"

    local Resource1 = Instance.new("IntValue", ResourceFolder)
    Resource1.Name = "Wood"
    Resource1.Value = 0

    local Resource2 = Instance.new("IntValue", ResourceFolder)
    Resource2.Name = "Rock"
    Resource2.Value = 0

    local Resource3 = Instance.new("IntValue", ResourceFolder)
    Resource3.Name = "Sticks"
    Resource3.Value = 0

    local Resource4 = Instance.new("IntValue", ResourceFolder)
    Resource4.Name = "Stones"
    Resource4.Value = 0

    local Resource5 = Instance.new("IntValue", ResourceFolder)
    Resource5.Name = "Metal"
    Resource5.Value = 0

    local Resource6 = Instance.new("IntValue", ResourceFolder)
    Resource6.Name = "Gold"
    Resource6.Value = 0

    local Resource7 = Instance.new("IntValue", ResourceFolder)
    Resource7.Name = "Steel"
    Resource7.Value = 0

    local Resource8 = Instance.new("IntValue", ResourceFolder)
    Resource8.Name = "Bronze"
    Resource8.Value = 0

    local Resource9 = Instance.new("IntValue", ResourceFolder)
    Resource9.Name = "Adurite"
    Resource9.Value = 0

    local Resource10 = Instance.new("IntValue", ResourceFolder)
    Resource10.Name = "Viridian"
    Resource10.Value = 0

    local Resource11 = Instance.new("IntValue", ResourceFolder)
    Resource11.Name = "Black Iron"
    Resource11.Value = 0

    local Resource12 = Instance.new("IntValue", ResourceFolder)
    Resource12.Name = "Bombastic"
    Resource12.Value = 0

    local Resource13 = Instance.new("IntValue", ResourceFolder)
    Resource13.Name = "Laser"
    Resource13.Value = 0

    local Resource14 = Instance.new("IntValue", ResourceFolder)
    Resource14.Name = "Gamma"
    Resource14.Value = 0

    local Resource15 = Instance.new("IntValue", ResourceFolder)
    Resource15.Name = "Tech"
    Resource15.Value = 0

    local Resource16 = Instance.new("IntValue", ResourceFolder)
    Resource16.Name = "Mythium"
    Resource16.Value = 0
end)

Any help is appreciated.

[TL;DR VERSION:] I'm saving multiple values in a datastore and it went wrong. above is the script, i don't know what to do.

0
Save a table full of stuff into dataStore. then use :GetAsync() to get the table of stuff. theCJarmy7 1293 — 7y
0
What in the world.... That script has so many unnecessary lines. Use tables bro. KingLoneCat 2642 — 7y

Answer this question