Is there any other way to improve and make my code any more simple than it is now?
Script
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 = {
{"Coins","NumberValue",0};
};
function PlayerAdded(Player)
local leaderstats = Instance.new("Folder");
leaderstats.Name = "leaderstats";
leaderstats.Parent = Player;
for i,v in next,Stats do
local Value = Instance.new(v[2]);
Value.Name = v[1];
Value.Value = v[3];
Value.Parent = leaderstats;
end
DataStoreModule.LoadData(Player,leaderstats,StatsData);
end
function PlayerLeft(Player)
pcall(function()
StatsData:SetAsync(Player.UserId,DataStoreModule.SaveData(Player.leaderstats));
end)
end
function Shutdown()
local Players = {};
for i,v in next,game.Players:GetChildren() do
PlayerLeft(v);
table.insert(Players,v);
end
for i,v in next,Players do
game:GetService("TeleportService"):Teleport(game.PlaceId,v);
end
end
game.Players.PlayerRemoving:Connect(PlayerLeft);
game.Players.PlayerAdded:Connect(PlayerAdded);
game:BindToClose(function()
Shutdown();
while wait() do
-- keep the server run as longest possible
end
-- ahh we are all gonna die
end)
DataStoreModule
This is the script that handles the Data Load and Save.
local function LoadData(Player,Inventory,Data)
local UserData;
pcall(function()
UserData = Data:GetAsync(Player.UserId);
end)
if (UserData) then
for i,v in next,UserData do
--[[
v = {name = v.Name;value = v.Value};
--]]
if (Inventory:FindFirstChild(v.name)) then
Inventory[v.name].Value = v.value;
else
warn(v.name.." is not a valid member of "..Inventory.Name)
end
end
end
end
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};
table.insert(DataSave,TempData);
end
return DataSave;
end
local Module = {};
Module.LoadData = LoadData;
Module.SaveData = SaveData;
return Module;
function Shutdown()
-- Save their data first off like you have.
for i,v in next,game.Players:GetChildren() do
PlayerLeft(v);
end
-- Then rather than teleporting them back to the place. Try this..
for _,player in next,game.Players:GetPlayers() do
player:Kick("Reason here")
end
-- 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:
end
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.
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.
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.
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.
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.