Hello,
So I am currently rewriting my DataStore
ModuleScript
, my older version was very Messy and had a lot of inconsistencies, and this may have some too, and now I am currently in part with Retrieving the Data from DataStoreService
.
I am Aware when doing this, I must use a pcall()
to put the code under a protected mode to prevent errors, and “appearently” you have better access to the Service, Looking at the Arguments of pcall()
, There is only:
-
function()
Thefunction()
to put under protection mode
And you could use it like this:
local Success, Err = pcall(function()
Code()
end)
if Success then
FireCode()
elseif not Success then
FireError()
end
ypcall()
is a Deprecated Global, Apparently It’s an older version of what is now pcall()
, Assuming it does the exact same thing, It should be:
local Success, Err = ypcall(function()
Code()
end)
if Success then
FireCode()
elseif not Success then
FireError()
end
But what about xpcall()
, It has a Custom Error Handler?
Looking at its Arguments, It does:
-
function()
Thefunction()
to put under protection mode -
Error
Thefunction()
for when an error Occurs
The way it could be used is:
local Success, Data = xpcall(function()
return Code()
end, function()
FireError()
end)
if Success then
FireCode()
end
These are just a Couple of Uses for pcall()
, ypcall()
, and xpcall()
,
But which would be better to use when Handling DataStores?
These are Two Versions of my Code utilizing both pcall()
, and xpcall()
:
xpcall()
version:
function Profile:Load(Plr: Player)
local Success, Data = xpcall(function()
return Profile.DataStore:GetAsync("Player_"..Plr.UserId)
end, function()
Plr:Kick("Failed to Access DataStores")
end)
if Success then
if Data then
Profile.Session[Plr.Name] = Data
else
Profile.Session[Plr.Name] = {
["Level"] = 1;
["EXP"] = 0;
["Cash"] = 0;
["Item"] = {};
["Banned"] = false
}
end
end
Profile.Stats(Plr)
end
Standard pcall()
version:
function Profile:Load(Plr: Player)
local Success, Data = pcall(function()
return Profile.DataStore:GetAsync("Player_"..Plr.UserId)
end)
if Success then
if Data then
Profile.Session[Plr.Name] = Data
else
Profile.Session[Plr.Name] = {
["Level"] = 1;
["EXP"] = 0;
["Cash"] = 0;
["Item"] = {};
["Banned"] = false
}
end
else
Plr:Kick("Failed to Access DataStores")
end
Profile.Stats(Plr)
end
Both xpcall()
and pcall()
Appear to be Valid Options with this use, Which would be more beneficial with Handling DataStores?
Can you Help?
Thanks.