Hey! I'm having trouble with something regarding Data Stores, I know how they work and everything(although confusing sometimes). However, i need to store a large amount of data quickly and easily in a way where i don't have to really change the storage script if i change anything in the values or add any values. I have attempted using tables, and although i am more knowledgeable with tables and arrays, i cant seem to figure this one out. Does anyone have any thoughts of how to store a whole folder worth of data? There is another folder full of values inside the main folder, and those values also have children regarding those values. Thanks! -Manelin
local DSService = game:GetService('DataStoreService'):GetDataStore('MagicalnessTesting') --Change this name to a random name or it will not work local Variables = {} game.Players.PlayerAdded:connect(function(plr) -- Define variables local uniquekey = 'id-'..plr.userId local MagicValues = game.ServerStorage:FindFirstChild("MagicValues"):Clone() MagicValues.Parent = plr print("Put MagicValues in player", plr.MagicValues) local MagicSpells = MagicValues.Spells wait(2) for _, Values in pairs(MagicValues:GetDescendants()) do if Values:IsA'StringValue' or Values:IsA'NumberValue' or Values:IsA'BoolValue'then table.insert(Variables, Values.Name) Variables[Values.Name] = tostring(Values.Value) print(Variables[Values.Name].."Vairables") end end -- GetAsync local GetSaved = DSService:GetAsync(uniquekey) if GetSaved then for i = 1, #Variables do Variables[i].Name = GetSaved[Variables] end else for i = 1, #Variables do DSService:SetAsync(uniquekey, Variables) end end end) game.Players.PlayerRemoving:connect(function(plr) local uniquekey = 'id-'..plr.userId local MagicValues = plr.MagicValues local Savetable = {} for _, Values in pairs(MagicValues:GetDescendants()) do if Values:IsA'StringValue' or Values:IsA'NumberValue' or Values:IsA'BoolValue'then table.insert(Savetable, Values.Name) Savetable[Values.Name] = tostring(Values.Value) DSService:SetAsync(uniquekey, Savetable) end end end)
You can create an entire player's save in a model, like I've done. Whenever you want to add something new, just add another instance value in the model. (Like number value, bool value, stringvalue, etc)
Create the model with everything you want to save and parent it to the script. In my example, i named my model "PlayerSave"
In explorer, it would look like this:
ServerScriptService: script PlayerSave ValueOne ValueTwo ValueThree
local PlayerSavedData = game:GetService('DataStoreService'):GetDataStore("TEST") game.Players.PlayerAdded:connect(function(player) --When the player joins repeat wait() until script.PlayerSave --wait until the model is actually loaded local save = script.PlayerSave:Clone()--Create the PlayerSave Values save.Parent = player --Put the model of values inside the player end)
Then, make the table for everything in your model you made, you will have to add to this table if you added another value instance to your model.
function PlayerDataTable(player) --Name of your table (it can be anything) local savevalues = { --in this example, i have 3 values. you can add more, just add to the list when you want. ValueOne = player.PlayerSave.ValueOne.Value, ValueTwo = player.PlayerSave.ValueTwo.Value, ValueThree = player.PlayerSave.ValueThree.Value } return savevalues end
Then, you're going to add the player's save onto them when they join.
game.Players.PlayerAdded:connect(function(player) repeat wait() until player.Character repeat wait() until player:FindFirstChild("PlayerSave") --wait until the player has their save model local data = PlayerDataTable(player) local playerkey = "player-"..player.userId --the individual player themselves local SaveFiles = PlayerSavedData:GetAsync(playerkey) --get the player's save if SaveFiles==nil then print("player has no save yet") --if the player is new else --if the player has a save player.PlayerSave.ValueOne.Value = SaveFiles.ValueOne player.PlayerSave.ValueTwo.Value = SaveFiles.ValueTwo player.PlayerSave.ValueThree.Value = SaveFiles.ValueThree --Puts the save file inside each value. Add to this list when you want to add another value to save. end)
Saving just works the same way, but add the table.
function SaveData(player) local playerkey = "player-"..player.userId --individual user local data = PlayerDataTable(player) --the table PlayerSavedData:SetAsync(playerkey,data) --save the table to the user end
Save the entire player's save when they leave the game.
game.Players.PlayerRemoving:Connect(SaveData)