Pcalls - When and how to use them

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.

1 Like

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

Very good tutorial goes in depth and explains well!!!