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 index
and 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.
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.