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.

4 Likes

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

3 Likes

@ReturnedTrue can you look into this?

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

1 Like

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.

2 Likes

Can I have two functions in a pcall?

pcall(function()

pointsStore:SetAsync()
coinsStore:SetAsync()

end)

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

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

5 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

1 Like

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.

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?

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

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

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.

1 Like

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

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.

2 Likes

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

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

2 Likes