Recently, I’ve been reading up on data stores in Roblox. I understand why you need to use pcall
with data stores – my real problem resides with Roblox’s examples of code displaying pcall
working with data stores.
Under Reading Data in Data Stores, Roblox uses pcall
to check if :GetAsync()
throws an error:
local DataStoreService = game:GetService("DataStoreService")
local experienceStore = DataStoreService:GetDataStore("PlayerExperience")
local success, currentExperience = pcall(function()
return experienceStore:GetAsync("User_1234")
end)
if success then
print(currentExperience)
end
Why do they wrap experienceStore:GetAsync()
in another function that is wrapped by pcall
when they could’ve just wrapped experienceStore:GetAsync()
in pcall
without the other function like:
local success, currentExperience = pcall(experienceStore.GetAsync, experienceStore, "User_1234")
-- Colons can't be used in pcalls when calling the function, for anyone wondering why I used a period.
I’ve tried this method and honestly it’s way more readable this way. Why does the documentation do this?