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

Simpler way to save data with a for loop? my script is getting long (Reward: Rep and upvote)

Asked by 6 years ago

So, I have this MASSIVE script with 84 lines and every time I create an instance I need to put the name inside this script so... is there a way I can use a for loop instead?

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



game.Players.PlayerAdded:connect(function(player)
    -----------------------------------------------------------------------------------------------------
    local stats = Instance.new("IntValue", player)
    stats.Name = "SaveMonsters"


    local a = Instance.new("NumberValue", stats)
    a.Name = "monster1"
    local b = Instance.new("NumberValue", stats)
    b.Name = "raremonster"
    local c = Instance.new("NumberValue", stats)
    c.Name = "something"

    local d = Instance.new("NumberValue", stats)
    d.Name = "anotherthing"
    local e = Instance.new("NumberValue", stats)
    e.Name = "weirdstuff"
    local f = Instance.new("NumberValue", stats)
    f.Name = "anexample"

    local g = Instance.new("NumberValue", stats)
    g.Name = "stuff"
    local h = Instance.new("NumberValue", stats)
    h.Name = "randomstuff"
    local i = Instance.new("NumberValue", stats)
    i.Name = "idkwhatthisis"

    local j = Instance.new("NumberValue", stats)
    j.Name = "bruh"
    local k = Instance.new("NumberValue", stats)
    k.Name = "i"
    local l = Instance.new("NumberValue", stats)
    l.Name = "need"

    local m = Instance.new("NumberValue", stats)
    m.Name = "help"
    local n = Instance.new("NumberValue", stats)
    n.Name = "saving"
    local o = Instance.new("NumberValue", stats)
    o.Name = "this"
    local p = Instance.new("NumberValue", stats)
    p.Name = "stuff"
    local q = Instance.new("NumberValue", stats)
    q.Name = "pleaseHelp"
    local r = Instance.new("NumberValue", stats)
    r.Name = "andFreeRep"
    ----------------------------------------------------------------------------------
    local key = "player-"..player.userId



    local savedValues = DataStore:GetAsync(key)

    if savedValues then
        a.Value = savedValues[1]
        b.Value = savedValues[2]
        c.Value = savedValues[3]
        d.Value = savedValues[4]
        e.Value = savedValues[5]
        f.Value = savedValues[6]
        g.Value = savedValues[7]
        h.Value = savedValues[8]
        i.Value = savedValues[9]
        j.Value = savedValues[10]
        k.Value = savedValues[11]
        l.Value = savedValues[12]
        m.Value = savedValues[13]
        n.Value = savedValues[14]
        o.Value = savedValues[15]
        p.Value = savedValues[16]
        q.Value = savedValues[17]
        --Save format: {points, coins}
    else
        local valuesToSave = {a.Value,b.Value,c.Value,d.Value,e.Value,f.Value,g.Value,h.Value,i.Value,j.Value,k.Value,l.Value,m.Value,n.Value,o.Value,q.Value}
        DataStore:SetAsync(key, valuesToSave)
    end


end)


ugh, every time i make a new value in a folder, i need to add it to this place, I need to make this script simpler, I have tried using a for loop for datastore but EVERY TIME IT EITHER DOESNT SAVE OR IT GIVES ME A STINKIN ERROR WITH NO SOLUTION AGHH (I am going crazy, someone help me, I will give an upvote and rep to the person that answers this)

1 answer

Log in to vote
1
Answered by
tantec 305 Moderation Voter
6 years ago
Edited 6 years ago

You could use a for loop and by using a table, here's how I would do this

game.Players.PlayerAdded:connect(function(player)
dataNames = {"Example", "Stuff", "Everything"}
dataToSave = {}

local stats = Instance.new("IntValue", player)
stats.Name = "SaveMonsters"


for i = 1,#dataNames do
local l = Instance.new("NumberValue", stats)
l.Name = dataNames[i]
local key = "player-"..player.userId
local savedValues = DataStore:GetASync(key)
if savedValues then
l.Value = savedValues[i]
else
table.insert(dataToSave, l.Value)
end
end

if #dataToSave > 0 then
DataStore:SetASync(key, dataToSave)
end

end)


0
I had that prob thanks wilsonsilva007 373 — 6y
0
Tysm greatneil80 2647 — 6y
0
btw line 22 SetAsync()* greatneil80 2647 — 6y
0
I think savedValues[i] is depricated or something... 10:33:32.864 - Workspace.Save stats.Items:15: attempt to index local 'savedValues' (a number value) greatneil80 2647 — 6y
Ad

Answer this question