|
| 15 Apr 2016 10:29 AM |
So I have a Folder full of Values (game.Players.Player.Folder).
How would I save all values inside that folder, and later load those when the player re-joins the game?
|
|
|
| Report Abuse |
|
|
|
| 15 Apr 2016 10:39 AM |
Ideally you wouldn't save values in the folder at all. Doing so not only increases the time it takes to make changes to the values (which is largely unnoticeable for smaller games), but it also would not work with FE enabled; if FE is enabled, only the client can see the values, and there is the potential for the client to cheat and modify the values anyway, regardless of FE settings.
Combined with the fact that you want to save the values for later loading, it's not a very good idea to store these values in objects, for a few reasons. Firstly, if you use a table to hold all the stats of the player, copying the stats to these value objects in the folders could cause inconsistency; if you change values in the table, you must update the value objects in the player, and vice versa. If you're only using the value objects in the folder, you'll need to build a new table in order to save it *every time* which takes up a lot more time than just having the table already as in the first scenario.
So either way, it's not recommended to use value objects to store stats. Instead, store them in a table in your script, then use Remote/BindableFunctions to retrieve the values that belong to a player. This also makes it very easy to save the values to DS because they are already represented as data in a table, which can be directly saved without having to worry about building any save data from scratch. |
|
|
| Report Abuse |
|
|
|
| 15 Apr 2016 10:43 AM |
I'm trying to help someone out with their game, and they already have a full system which uses the values from the folder to run anything. Therefore I don't want to spend all the time he has spent on writing the code for it, to rewrite it into Table values.
So lets just stick to the Values stored in the Player.Folder, there's about 20 of them, and I'd like to Save them all after the player leaves, and Load them when the player comes back into the game.
|
|
|
| Report Abuse |
|
|
| |
|
|
| 15 Apr 2016 11:02 AM |
Fair enough, but it would be worth it to convert the code to use straight table values, so keep that in mind.
As for saving, what I recommend is building a table and saving the table directly to DS.
local DSS = game:GetService("DataStoreService") local DS = DSS:GetDataStore("playerstats")
local StatsKey = "%d_stats" -- [USERID]_stats
local LoadControl = {} -- Prevent loading/saving of default values (false, "", 0)
function GetKey(Player) return StatsKey:format(Player.UserId) end
function LoadPlayer(Player) -- Assumes all stat values are created in the player already local Stats = DS:GetAsync( GetKey(Player) ) for k,v in pairs(Stats) do Player.Folder[k] = v end LoadControl[Player.UserId] = true end
function PlayerHasLoaded(Player) return LoadControl[Player.userId] end
function Build(Player) -- Assumes Player.Folder will always exist and only Values are in here local t = {} for _,v in pairs(Player.Folder:GetChildren()) do t[v.name] = v.Value end return t end
function Save(Key, Table) -- We assume old values don't matter since we're saving new values DS:SetAsync(Key, Table) end
function BuildAndSaveDataTable(Player) local PlayerData = Build(Player) local PlayerKey = GetKey(Player) Save(PlayerKey, PlayerData) end
-- Load player data upon entering game.Players.PlayerAdded:connect(function(p) -- Create the values and Folder object here
LoadPlayer(p) end)
-- Save player data upon leaving game.Players.PlayerRemoving:connect(function(p) -- Check if data has been loaded; if so, save if (LoadControl[p.UserId]) then BuildAndSaveDataTable(Player) end
-- If the data has not been loaded but is saved anyway, default values will replace actual values -- This is why we added the load check above to only save if the data was fully loaded -- Now we modify the LoadControl table to signal that the data is no longer loaded (player left) LoadControl[p.UserId] = nil end)
As usual of my posts, this code is not tested, so please test it in a dummy place so actual save data is not modified. |
|
|
| Report Abuse |
|
|