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 7 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?

01local DataStore = game:GetService("DataStoreService"):GetDataStore("Munster")
02 
03 
04 
05game.Players.PlayerAdded:connect(function(player)
06    -----------------------------------------------------------------------------------------------------
07    local stats = Instance.new("IntValue", player)
08    stats.Name = "SaveMonsters"
09 
10 
11    local a = Instance.new("NumberValue", stats)
12    a.Name = "monster1"
13    local b = Instance.new("NumberValue", stats)
14    b.Name = "raremonster"
15    local c = Instance.new("NumberValue", stats)
View all 83 lines...

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
7 years ago
Edited 7 years ago

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

01game.Players.PlayerAdded:connect(function(player)
02dataNames = {"Example", "Stuff", "Everything"}
03dataToSave = {}
04 
05local stats = Instance.new("IntValue", player)
06stats.Name = "SaveMonsters"
07 
08 
09for i = 1,#dataNames do
10local l = Instance.new("NumberValue", stats)
11l.Name = dataNames[i]
12local key = "player-"..player.userId
13local savedValues = DataStore:GetASync(key)
14if savedValues then
15l.Value = savedValues[i]
View all 25 lines...
0
I had that prob thanks wilsonsilva007 373 — 7y
0
Tysm greatneil80 2647 — 7y
0
btw line 22 SetAsync()* greatneil80 2647 — 7y
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 — 7y
Ad

Answer this question