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

Is there a way to label the index of a table using a loop? [DataStore Question]

Asked by
Radstar1 270 Moderation Voter
6 years ago

BACKGROUND

So right now I am trying to make my DataStore more efficient, and to save some time from me having to insert every value into a table myself.

In order to save data in my game I make a dictionary then label the indexand the value associated with it. For Example:

local SaveFile = {
            ShirtValue = CharacterSave.ShirtValue.Value ,

            PantsValue = CharacterSave.PatsValue.Value
            }
MemoryCard:SetAsync(Player.userId,SaveFile)

Then in order to retrieve the data I loop through the folder I saved everything in and apply the value for the specific name.

 local save = Player:WaitForChild("CharacterSave");  --Folder with values
             for i,v in next,PlayerData do -- PlayerData is the Datastore
                    save:WaitForChild(i).Value = v;
             end

As you can see in the dictionary I have to add each value one by one in order to save each specific key for value.

Question Is there a way I can label the dictionary by looping through my charactersave folder?

Thanks for reading.

1 answer

Log in to vote
0
Answered by 6 years ago

Yes:

local SaveFile = {}
for i, v in ipairs(Player:WaitForChild("CharacterSave"):GetChildren()) do
    SaveFile[v.Name] = v.Value
end

Note: In a for loop, pairs(t) is the same as next, t. ipairs is the same as pairs if you're iterating over a list.

0
Honestly man, you have saved my script so many lines, and for me; so much time. Thank you for this answer. So basically saying SaveFile[v.Name] changes the indexes name to whatever is in my folder's name. Radstar1 270 — 6y
Ad

Answer this question