Those with "finished" games - How often do you use PCalls and for what?

So, I’ve got miles of code written, my game is finally not buggy, I don’t have that much more to do…

But I have very few PCalls… and I’m sure that’s wrong.

Can you guys let me know what you PCalls for?

I know they are used to generate error messages, and are used so that your game doesn’t crash if there is an issue…

But where are ALL the places you would recommend that I use them.

Only on calls to Datastore?

Or elsewhere?

Thanks much for the help, as I don’t want the game to crash…

(oof derp)

You should only use pcalls when you’re creating a function or sending a request that can possibly error. For example, if you send a web request to your personal servers on your website, that could possibly fail for whatever reason. People usually use pcalls so they can create a “backup” when something fails.

from the HttpService::RequestAsync developer hub page

local HttpService = game:GetService("HttpService")
 
local function request()
	local response = HttpService:RequestAsync(
		{
			Url = "http://httpbin.org/post",  -- This website helps debug HTTP requests
			Method = "POST",
			Headers = {
				["Content-Type"] = "application/json"  -- When sending JSON, set this!
			},
			Body = HttpService:JSONEncode({hello = "world"})
		}
	)
 
	-- Inspect the response table
	if response.Success then
		print("Status code:", response.StatusCode, response.StatusMessage)
		print("Response body:\n", response.Body)
	else
		print("The request failed:", response.StatusCode, response.StatusMessage)
	end
end
 
-- Remember to wrap the function in a 'pcall' to prevent the script from breaking if the request fails
local success, message = pcall(request)
if not success then
	print("Http Request failed:", message)
end

The code above sends an Http request to an external website. The pcall is there so that the rest of the script doesn’t break if the request fails.

You really shouldn’t put yourself in a position where need to use a lot of pcalls unless absolutely necessary.

For a Roblox function, whenever you see “Async,” that typically means that it’s gonna do an external web call and you should wrap it in a pcall.

1 Like

Only when required. If something has a chance of provoking an error, such as getting save data, http requests, etc., I use pcall. There shouldn’t be any other cases for using pcall.

2 Likes

Any calls to an external API

Datastores, getting product info, game info, player info etc if it isn’t already in the game when the user joins.

3 Likes

Super big thanks to all responses. I appreciate the friendly help like I appreciate a good bowl of chili cheese dip. That means I appreciate it a lot.


P.S - Is it possible to click more than one reply as “solution”?

(oof - derp)

1 Like

There is not, I recommend picking one that best answers your question.

1 Like

I rolled a die. 1,2 - 3,4 - 5,6

Rolled a 5, so 3rd answer got it. But all got “Likes”. And many thanks.

(oof - derp)

2 Likes