Pcalls - When and how to use them

In the OP’s " DataStore errors - retrying" section, both sections of code have a variable called “data” that I’m assuming that should be the variable “response” instead?

2 Likes

Ah something from the previous code revision, thanks for bringing my attention to it.

5 Likes

Hey, sorry to bump the thread back up, I just thought it would be necessary to put this on here. Should most marketplace functions use pcalls, and which ones?

1 Like

I would use pcalls on the ones that return information.

5 Likes

@COUNTYL1MITS is absolutely correct, any of those which return information fetch from the web APIs.

4 Likes

@ReturnedTrue can you look into this?

I also wonder if InsertService.LoadAsset can fail in normal circumstances

3 Likes

We think alike. I’m certain it could potentially fail although I haven’t seen that occur.

LoadAsset can definitely fail, I’ve had on multiple occasions.

3 Likes

Can I have two functions in a pcall?

pcall(function()

pointsStore:SetAsync()
coinsStore:SetAsync()

end)
1 Like

yes, but if pointStore:SetAsync() fails then coinsStore:SetAsync() will not be run either, it can only catch one error per pcall, and can call many functions

1 Like

This has been a very useful resource for my programming on roblox. Thank you for making it.

6 Likes

Are there any other usages for pcall besides using it on functions for return information fetch from the web APIs? Are pcalls only used for those functions? Thanks

2 Likes

You can use pcalls for anything, especially when there is a chance of an error being thrown. I personally use pcalls for my own functions to return statuses and results.

1 Like

For every function that returns values? Instead of using pcalls, can you simply check if returnedvalue ~= nil then end for functions that don’t correlate with API?

1 Like

You can simply check that; however, some functions will emit errors, stopping the script, so pcalls can help prevent the script from stopping entirely.

1 Like

Oh I see, can you give some examples? Thanks!

1 Like

A specific example of a valid pcall that does not use a web call is ComputeAsync(), which is used for pathfinding. An error is thrown if the navigation mesh has no vertices to reference to pathfind through.


[Side Note]
This is the most expensive non-webcall method I’ve come across; but this isn’t surprising considering how much it is doing! Most programmers elect to limit the amount of times ComputeAsync() is called by using raycasting or distance/position checks; never call this every frame.

[Edit]
Removed the part about the wording of an asynchronous name since the wording is chosen to relay what is expected when it is called.

2 Likes

i dont get it. How can i use it? Is it a call or fire event or naming it? why . insted of : ?

1 Like

they retrieve a method that datastore (DataStore:GetAsync() in this case) has and then pass explicit self into that method.
Basically. pcall(DataStore.GetAsync, DataStore, "key") is same as protected DataStore.GetAsync(DataStore, "key"), which is same as DataStore:GetAsync("key") (and also protected aswell because its all in pcall)
you could of course use a dumbed down version and use a wrapper function provided below

local success, result = pcall(function()
    return DataStore:GetAsync("key")
end)

or just write the result into a pre-defined local as mentioned before.

TL;DR: . is used instead of : because we need to obtain a method to use pcall on and then explicitly pass the object itself to imitate a method call.

3 Likes

i get it now, bassicaly it works like a module or function. But thx i already know it

1 Like

very informative and clear tutorial. really appreciate this, thank you

3 Likes