Im doing it this way, but is there a more efficient way to do this?
local DSS = game:GetService("DataStoreService") local dataStore = DSS:GetDataStore("dataStore") game.Players.PlayerAdded:Connect(function(plr) local folder = Instance.new("Folder",plr) folder.Name = "leaderstats" local inventory = Instance.new("Folder",plr) inventory.Name = "Inventory" local coins = Instance.new("IntValue",folder) coins.Name = "Coins" local xp = Instance.new("IntValue",folder) xp.Name = "XP" local crops = Instance.new("IntValue",inventory) crops.Name = "Crops" local dataCoins local dataXP local dataCrops dataCoins = dataStore:GetAsync(plr.UserId.."-Coins") dataXP = dataStore:GetAsync(plr.UserId.."-XP") dataCrops = dataStore:GetAsync(plr.UserId.."-Crops") coins.Value = dataCoins xp.Value = dataXP crops.Value = dataCrops end) game.Players.PlayerRemoving:Connect(function(plr) dataStore:SetAsync(plr.UserId.."-Coins",plr.leaderstats.Coins.Value) dataStore:SetAsync(plr.UserId.."-XP",plr.leaderstats.XP.Value) dataStore:SetAsync(plr.UserId.."-Crops",plr.Inventory.Crops.Value) end)
Generally a good data system does not have any objects, just a modulescript and a mediator between the receivers and core script.
What you tend to do is have a modulescript where you load and store the player's data upon them joining the game, that you then temporarily save in a table to easily access and change it then save the table back to the corresponding datastore when they exit the game.
The ModuleScript should have a Script that manages all the functions allowed to be sent towards it, a RemoteEvent/Function and a bindablefunction to connect all the requests together and manage them.
The remotefunction/RemoteEvent ( client to server communication ) should only have access to obtaining the data and not changing it, the client being able to change the data would ruin the entire system. You can use a remote event which is a lot safer but a tad more difficult to program than a remote function, if you're smart about it you can easily do it.
The BindableFunction (server to server communcation) should be able to both obtain and change data as you see fit, since the server side should already be secured.
Make sure all that is obtained and changes are the tables that are then SetAsync when the player disconnects.
This is my current setup and it works very well.