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.
What if so I made BoolValue called DataLoaded if DataStore throws errors I won’t make the value true if it loads then I will set the value to true. When a player leaves it will check if DataLoaded value is set to true if it is set to true, then it will save the data if not then it won’t save it and the player will keep his old data. How does this sounds?
local Load = function(folder,data,plr)
local Succsess,Data = pcall(function()
return data:GetAsync(plr.UserId);
end)
if (not Succsess) then
print("faild to load data");
return;
end
if (Data == nil) then
return;
end
plr.DataLoaded.Value = true;
for i,v in next,Data do
print(v.name)
if (folder:FindFirstChild(v.name)) then
folder:FindFirstChild(v.name).Value = v.value;
else
print(v.name.." is not valid member of "..folder.Name);
end
end
end
I am running into an issue now If a player is new to the game and I load data it would be nil and if datastore throws error error it would also be nil how would I detect if the player is new to the game and the data is nil because of that or because the datastore did not load successfully.
local Module = {}
function Module:Load(plr,leaderstats,GDS)
local Succsess,Data = pcall(function()
return GDS:GetAsync(plr.UserId);
end)
if (Succsess and Data) then
for i,v in next,Data do
if (leaderstats:FindFirstChild(Data.name)) then
leaderstats:FindFirstChild(Data.name).Value = leaderstats:FindFirstChild(Data.name) + v.value;
else
warn(v.name.." is not valid member of "..leaderstats.Name);
end
end
plr.DataLoaded.Value = true;
else
plr.DataLoaded.Value = false;
end
end
function Module:Save(leaderstats)
local Data = {};
for i,v in next,leaderstats:GetChildren() do
local TempData = {
name = v.Name;
value = v.Value;
};
table.insert(Data,TempData);
end
return;
end
return Module;
function Module:Load(plr,leaderstats,GDS)
local Succsess,Data = pcall(function()
return GDS:GetAsync(plr.UserId);
end)
if (Succsess and Data == nil) then
plr.DataLoaded.Value = false;
end
if (Succsess and Data) then
plr.DataLoaded.Value = true;
for i,v in next,Data do
if (leaderstats:FindFirstChild(Data.name)) then
leaderstats:FindFirstChild(Data.name).Value = leaderstats:FindFirstChild(Data.name) + v.value;
else
warn(v.name.." is not valid member of "..leaderstats.Name);
end
end
end
end