How and in what situations do i use Pcall?

Probably time i should know what this does so

What is Pcall and how does it work

Also in what situations should i be using it in?

1 Like

Datastores, Pcalls are extremely usefull for them

1 Like

In occasions where you’re dealing with functions that are known to throw errors, as @Koiyukki said, datastore modification is one of the main use case of pcall, but the function itself should be used when you do not know when a function can throw.

In addition to DataStores, you’d want to use them in anything related to the web, whether it’s a web function signal, etc.

Examples:
GetRankInGroup, GetRoleInGroup, UserHasBadgeAsync, GetGroupInfoAsync, etc…

Because they are web signals, when the web is down/ having issues, those functions won’t work properly, they’d have little issues due to the web being down.

Simple Example

local success, result = pcall(function()
	return something(x,y,z)
end)

if success then -- if our function didnt fail
   print(result) -- that'd print whats inside the fucntion we just called above
else
   warn(result) -- if failed, it'd throw a warning.
end
1 Like

Also pretty sure GetFriendsOnline as well, but yeah functions like such.

1 Like

if you have a function that has a chance to throw an error and you dont want that error to stop from executing rest of the code you should wrap that function in pcall

local Success, ErrorMessage = pcall(someFunction, arg1, arg2, arg3)

or

local Success, returnValue = pcall(function()
	return someFunction(arg1, arg2, arg3)
end)
if Success then -- that function didnt throw an error
	print(returnValue) -- if the function returns anything
esle -- if not success that means returnValue is error message
	warn(returnValue)
end
2 Likes