Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
COMMUNITY QUESTION

Whats the third paremeter of pcall and what does it do?

	local success, result, Plays = pcall(handler, ReceiptInformation,player)



I saw a code like this on the wiki and i was wondering what player was

1 answer

I believe this is a snippet from the a MarketplaceService article. This third argument isn't specific to a Player Object, it actually represents the argument being supplied into the function defined in the first.

This is a bit tough to grasp, however, let me try my best to explain...

The most common representations of pcalls usually just receive a fresh function, of which we use to perform our instructions, E.g:

local Success, Response = pcall(function()
	print("Do something")
end)

If you're unfamiliar with what this first argument is, the example above should be quite obvious, pcall() will take the function provided and run it in what we call a try except block within other coding languages. Essentially, it will operate the instructions in a safe environment, meaning any potential exceptions thrown will not interrupt the Lua thread.

However, with your case above, you're using a more advanced form of a pcall() use. The structure holds itself like so: Function, Function Family; where it belongs to, & the argument that will by supplied when running it.

Let's take a look at an example of this with a DataStore, that way, we can make it a little easier to understand:

Note:

If there are no issues, Response is the variable :GetAsync() will return to.

local DataStoreService = game:GetService("DataStoreService")
local DataStore = DataStoreService:GetDataStore("MyData")

local Players = game:GetService("Players")

local Data;

local function GetData(Player)
	local Success, Response = pcall(
		DataStore.GetAsync, --// Function
		DataStore, --// Belongs to our DataStore Object
		Player.UserId --// Argument supplied
	)
	if (Success) then Data = Response end
end

Players.PlayerAdded:Connect(GetData)

If this helped, don't forget to accept this answer!

2 comments

0

Hi! I've always just thought pcall was used to prevent erroring for lazy people. Why are there two variables connected to pcall?

0

pcall will return a total of two pieces of information, a boolean representing if the function ran successfully (true or false) and a Response. The Response can be nil, or contain the information the successful code returns, be it any value. Otherwise, Response will be the error that occurred, you can print it to see what went wrong.

This discussion is preserved as it was. New answers, comments, and votes are closed.