I have seen a lot of people getting confused on how to efficiently manage and save data, so I thought I’d go over the theory and basics behind it.
I am by no means an expert, so take this with a grain of salt.
Feel free to correct or improve any of my points!
So, What is a Session System?
We’ll trim over the basics first.
A session usually refers to limited communication time between a client and the server.
For instance, having a temporary table of the player’s data when they’re in the game.
Whenever a player leaves, the connection gets cut off, and thus the session ends.
Visualized in code, it would look like this (my usage of semi-colons is just a preference, please don’t mind it);
local Players = game:GetService'Players';
local sessionData = {};
Players.PlayerAdded:Connect(function(Player)
--// Player joined, create a new session!
sessionData[Player.UserId] = {};
end);
Players.PlayerRemoving:Connect(function(Player)
--// Player left, remove the player's session.
sessionData[Player.UserId] = nil;
end);
Now, how can this be useful?
Well, data stores have a max upload limit (not to mention it’s yield).
This means that updating the data store directly each time a player does anything would result in an instant clogging of the data store.
The concept of data stores is simple - update a player’s value in our temporary session data table!
Tables obviously wouldn’t over-clog anything, and they’re very efficient!
The concept of session systems is very simple, and it’s point is to reduce clogging to a minimum.
You would only have to use the data store twice - getting the player’s data when they join, and saving the player’s data when they leave.
Visualized, it would look like this;
local Players = game:GetService'Players';
local dataStoreService = game:GetService'DataStoreService';
local exampleStore = dataStoreService:GetDataStore'Example Store';
local sessionData = {};
Players.PlayerAdded:Connect(function(Player)
--[[If the player has no saved data in
the data store (new player for example),
then just set it to an empty table.
]]
sessionData[Player.UserId] = exampleStore:GetAsync(Player.UserId) or {};
end);
Players.PlayerRemoving:Connect(function(Player)
--[[Player is leaving. Save their data
to the session data they had in the game.
]]
exampleStore:SetAsync(sessionData[Player.UserId]);
sessionData[Player.UserId] = nil;
end);
...
--// Changing some value would look like this!
sessionData[Player.UserId].Coins = 1000;
And it is as simple as that.
Simplified - you can use a temporary data table to update a player’s stats, and when a player leaves, simply set the player’s data store data as that table.
Side-note: please don’t forget to use game.BindToClose
, saving every player’s data when a server is about to close. Otherwise when a server closes, this can cause huge problems!
side-side note: if you know what I based my title off of, bonus points to you
2021 update:
Alright, so after having this being bumped, I’d heavily recommend to just use ProfileService. Insanely useful module which implements session locking I talked about! This tutorial is still of some use if you want to understand a bit about data structure