Is there any other way to improve my DataStore more?



Is there any other way to improve and make my code any more simple than it is now?


local sp = script.Parent;
local Main = sp.Parent;
local Modules = Main:WaitForChild("Module");
local DataStoreSettings = require(Modules:WaitForChild("Settings"));
local StatsData = game:GetService("DataStoreService"):GetDataStore(DataStoreSettings.StatsDataStore);
local DataStoreModule = require(Modules:WaitForChild("DataStore"));

local Stats = {

function PlayerAdded(Player)
	local leaderstats ="Folder");
	leaderstats.Name = "leaderstats";
	leaderstats.Parent = Player;
	for i,v in next,Stats do
		local Value =[2]);
		Value.Name = v[1];
		Value.Value = v[3];
		Value.Parent = leaderstats;

function PlayerLeft(Player)

function Shutdown()
	local Players = {};
	for i,v in next,game.Players:GetChildren() do
	for i,v in next,Players do


	while wait() do
		-- keep the server run as longest possible
	-- ahh we are all gonna die


This is the script that handles the Data Load and Save.

local function LoadData(Player,Inventory,Data)
	local UserData;
		UserData = Data:GetAsync(Player.UserId);
	if (UserData) then
		for i,v in next,UserData do
				v = {name = v.Name;value = v.Value};
			if (Inventory:FindFirstChild( then
				Inventory[].Value = v.value;
				warn(" is not a valid member of "..Inventory.Name)

local function SaveData(Inventory)
	local DataSave = {};
	for i,v in next,Inventory:GetChildren() do
		local TempData = {
			name = v.Name;
			value = v.Value;
		-- TempData = {name = v.Name;value = v.Value};

	return DataSave;

local Module = {};

Module.LoadData = LoadData;
Module.SaveData = SaveData;

return Module;

Data Key

return {
	StatsDataStore = "randkey";

Looks like



You should try not to make use of values as much.


Just a note here, on your Shutdown function…

function Shutdown()
        -- Save their data first off like you have.
        for i,v in next,game.Players:GetChildren() do
        -- Then rather than teleporting them back to the place. Try this..
        for _,player in next,game.Players:GetPlayers() do
               player:Kick("Reason here")
        -- I mean I understand what you are trying to do, but just use :Kick() -> wiki link above this code block.
        -- It just gives the user a reason and simple "reconnect" button when a new server comes up.
        -- Its just more efficient. :slight_smile: 


No need to call PlayerLeft. When the player leaves, it’d be automatically fired :slight_smile:


I just wanted to edit one part of the code lol. Primarily a better way to kick a player from the game.


Ok everyone is talking about this event and code inside in the event my question was how is the script built and the style.




Your LoadData function is dangerous. You pcall the GetAsync (good), but if it fails it just sets the player’s data to empty, and your SaveData function has no knowledge of whether or not the data its saving is their original data.

If data stores go down and people play your game, they will lose all their data.

Why do you have the loop? You should instead just make a BindableEvent and yield on it until all shutdown operations/saving finishes.


Ohhhhh, that is something that I have not thought of.


For shutting down a game I don’t recommend kicking the players, but teleport them to a reserved server (the same server, but really a private server) like this script that Merely did.

Soft Shutdown Script


Well, we keep the most of the players that were in the game.
So yeah, that is why I made so it teleports them to the same game.

but yes it kicks them again xD


But if you teleport them to the same game you could get in a loop of where you teleport the player in a server that’s trying to shutdown that script teleports the player in a reserved server then after like 10 seconds teleports them back to the main servers.


Your usage of SetAsync is uncomfortable. In addition to what Kampf mentioned, I personally would not use SetAsync unless there’s a reason I need to force data to be a certain way. I’d prefer using UpdateAsync to minimise issues while updating data - it does exist for a reason, as the name states, UpdateAsync. I find that Set is more for setting when it’s needed (i.e. PlayerData doesn’t exist).

Your shutdown function isn’t going to do much. All you’re doing is teleporting players back to the game. This doesn’t give the game a chance to shutdown old servers, especially since you’re hooking functions to OnClose (which allows a maximum of 30 seconds to finish operations binded to it before finally closing the server) - servers can be kept alive and negate the purpose of this function.

@Shadrz Why? If you’re going to suggest something, elaborate on your idea. Don’t just tell someone not to do something and forego reasoning.


I see this all the time when people use pcall. There is no need to wrap this in a function as pcall takes a function followed by any arguments to pass to that function.


You can use

--  you need to pass the table as : is used
pcall(StatsData.SetAsync, StatsData, Player.UserId, DataStoreModule.SaveData(Player.leaderstats))


What about this?



You would want to use two pcalls as you do not want to duplicate the request if one fails.


So what I’m trying to say is that he should use metatables, I’m not great at them but you should definitely put them in a module.


Metatables are not going to help in this use case. Metatables are not an alternative to ValueObjects, they’re intended to run metamethods on tables for certain cases, including the following cases:

  • If a member of the table is indexed or getting an extended index
  • Deciding what is to be done when a new value is created
  • Determining what should happen if a table variable is called like a function
  • etc.

See the metamethods Developer Hub article for more information on metamethods (since that’s the only real part of metatables you need to know, metatables are as simple as setmetatable(table, metatable)).

A proper alternative to ValueObjects is ModuleScripts, which OP is using aside from the actual registration of data which is loaded out across ValueObjects. Depending on OP’s use case and preferences, this advice could hold no value to them.


Oh ok, that makes sense.


My recommendation is what I usually do: Cache the data in a table. When I write Data Store modules I try to make as little use of “the Async’s” as possible.


So how can i fix that?