Ok this might be a little hard to explain. If you need to understand more in order to answer my question by all means ask. I'll try my best to explain. Ok so I have a script that creates 2 folders called RPGStats and leaderstats. You know that leaderstats is what roblox uses to hold stat amounts. In the script is my datastore stuff where it saves the values (so far I only have 1 value inside which is called bricks) but I also want to create a value in a seperate folder outside of leaderstats, can that info still be saved? Or does it need to be in the leaderstats
Here is my script
local DataStore = game:GetService("DataStoreService") local ds1 = DataStore:GetDataStore("ExampleDataStore") game.Players.PlayerAdded:connect(function(player) local leader = Instance.new("Folder",player) local RPGStats = Instance.new("Folder",player) leader.Name = "leaderstats" RPGStats.Name = "RPGStats" local Bricks = Instance.new("IntValue",leader) Bricks.Name = ("Bricks") local Level = Instance.new("IntValue",RPGStats) Level.Name = ("Level") Bricks.Value = ds1:GetAsync(player.UserId)or 0 ds1:SetAsync(player.UserId, Bricks.Value) Level.Value = ds1:GetAsync(player.UserId)or 1 ds1:SetAsync(player.UserId, Level.Value) Bricks.Changed:connect(function() print("Saving Data") ds1:SetAsync(player.UserId, Bricks.Value) print(player.UserId.."'s Date of "..Bricks.Value.." "..Bricks.Name.."Has been saved") end) end) game.Players.PlayerRemoving:connect(function(player) ds1:SetAsync(player.UserId, player.leaderstats.Bricks.Value) print(player.UserId.."'s Date of"..player.leaderstats.Bricks.Value.." "..player.leaderstats.Bricks.Name.."Has been saved") ds1:SetAsync(player.UserId, player.RPGData.Level.Value) print(player.UserId.."'s Date of"..player.RPGData.Level.Value.." "..player.RPGData.Level.Name.."Has been saved") end)
You care able to save any data to the data store as long as it fits within its limitations. Saving data only requires a key that is then associated with the data stored. The key is usually unique as we do not want to overwrite other players data.
The current code:-
-- This uses the same key so the same data will be returned Bricks.Value = ds1:GetAsync(player.UserId)or 0 ds1:SetAsync(player.UserId, Bricks.Value) -- This get request has been made within the 10 second cache time so the cached data would be returned Level.Value = ds1:GetAsync(player.UserId)or 1 ds1:SetAsync(player.UserId, Level.Value)
We can resolve this in two ways, the first being that we change the keys ( this would require another request) or we save the data in a table e.g:-
-- example one local tmp = {stat1.Value, stat2.Value, ...} -- example two local tmp = {['xp'] = some value, ['level'] = some value, ...} --- we cannot store mixed tables local tmp = {['xp'] = some value, [1] = some value}
Some useful links:- Table , Mixed Tables,Limitations
We can also change the key names:-
SetAsync('xp_' .. tostring(player.UserId)) -- other keys..
Then we just use the key associated with the data.
Back to the question, It is possible to save data at any time while the game is running (excluding request limits). As long as we save or know the key and still have the data we can save it.
Because the server can only save the data, if it has access to the data that needs to be saved there is no limitations on getting data from different parts of the game such as. The players leaderstats and the server storage.
I hope this helps, please comment if you need any more information. It may also help to look at my data store module to understand how to safely get and save data as there are a lot of potential problems that may occur when saving and loading player data.