Using pcall with Data Store Functions

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?

Tbh, I liked the first one because I can read it with ease rather than the long parameters because sometimes I used long variable names and it takes up my screen.

Also, I want the assurance of using the functions of something such as:

playerDS:GetAsync("key")

rather than putting it in the parameter as it may confusing to me.

That’s just my opinion though.

1 Like

Honestly, it just boils down to readability I guess, no other reason than that. Personally, I prefer my code not being too long on a single line so I use the former over the latter, but again, it’s just personal preference.

1 Like