Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
✓ ANSWER ACCEPTED

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

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.

1 answer

✓ Accepted answer

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.

1 comment

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.

This discussion is preserved as it was. New answers, comments, and votes are closed.