Simpler way to save data with a for loop? my script is getting long (Reward: Rep and upvote)
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?
01 | local DataStore = game:GetService( "DataStoreService" ):GetDataStore( "Munster" ) |
05 | game.Players.PlayerAdded:connect( function (player) |
07 | local stats = Instance.new( "IntValue" , player) |
08 | stats.Name = "SaveMonsters" |
11 | local a = Instance.new( "NumberValue" , stats) |
13 | local b = Instance.new( "NumberValue" , stats) |
14 | b.Name = "raremonster" |
15 | local c = Instance.new( "NumberValue" , stats) |
18 | local d = Instance.new( "NumberValue" , stats) |
19 | d.Name = "anotherthing" |
20 | local e = Instance.new( "NumberValue" , stats) |
22 | local f = Instance.new( "NumberValue" , stats) |
25 | local g = Instance.new( "NumberValue" , stats) |
27 | local h = Instance.new( "NumberValue" , stats) |
28 | h.Name = "randomstuff" |
29 | local i = Instance.new( "NumberValue" , stats) |
30 | i.Name = "idkwhatthisis" |
32 | local j = Instance.new( "NumberValue" , stats) |
34 | local k = Instance.new( "NumberValue" , stats) |
36 | local l = Instance.new( "NumberValue" , stats) |
39 | local m = Instance.new( "NumberValue" , stats) |
41 | local n = Instance.new( "NumberValue" , stats) |
43 | local o = Instance.new( "NumberValue" , stats) |
45 | local p = Instance.new( "NumberValue" , stats) |
47 | local q = Instance.new( "NumberValue" , stats) |
49 | local r = Instance.new( "NumberValue" , stats) |
52 | local key = "player-" ..player.userId |
56 | local savedValues = DataStore:GetAsync(key) |
59 | a.Value = savedValues [ 1 ] |
60 | b.Value = savedValues [ 2 ] |
61 | c.Value = savedValues [ 3 ] |
62 | d.Value = savedValues [ 4 ] |
63 | e.Value = savedValues [ 5 ] |
64 | f.Value = savedValues [ 6 ] |
65 | g.Value = savedValues [ 7 ] |
66 | h.Value = savedValues [ 8 ] |
67 | i.Value = savedValues [ 9 ] |
68 | j.Value = savedValues [ 10 ] |
69 | k.Value = savedValues [ 11 ] |
70 | l.Value = savedValues [ 12 ] |
71 | m.Value = savedValues [ 13 ] |
72 | n.Value = savedValues [ 14 ] |
73 | o.Value = savedValues [ 15 ] |
74 | p.Value = savedValues [ 16 ] |
75 | q.Value = savedValues [ 17 ] |
78 | 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 } |
79 | DataStore:SetAsync(key, valuesToSave) |
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)