Is pcall actually useful outside of datastores?

Hello! When I was creating a datastore I was wondering if pcall() can be and is used anywhere else outside of datastores?

2 Likes

Yes you can! pcall is commonly used when making HTTP requests and many other things.

2 Likes

As far as I know, yes. Cause they can filter out errors without making the script stop.

3 Likes

I use pcall for very important things so it doesn’t error. Like Datastores. Giving them points and stuff like that!

You can read this for further information: Pcalls - When and how to use them

3 Likes

Yes, you can use pcalls to detect if a function is successfull or errors. Here’s an example.

local Success, Func = pcall(function()
   print(game.Players.LocalPlayer.Character.Name)
end)

if Success then
   print("Works.")
end

those things mentioned above, yes, but some people also just use pcall() for their entire script to make a user-friendly Error System that prints errors in more easy-to-understand way.

that is an awful idea. first off it adds extra code for no reason and also decreases performance

yes, if your too lazy to script weather the player can be teleported

if not pcall(function()
player.Character.HumanoidRootPart.CFrame = game.Workspace.Platform.Position + Vector3.new(0,1.5,0)
end) then 
    print("Teleport failed!")
end

There’s several different issues you have to catch if you had just used

player.Character.HumanoidRootPart.CFrame = game.Workspace.Platform.Position + Vector3.new(0,1.5,0)
  1. The player’s character is nil
  2. The player’s HumanoidRootPart’s missing (if they fell into the void)
  3. The platform’s missing

Well this is just bad programming habit if anything.

2 Likes

pcall is just preventing errors from output,
pcall will return true if the code doesn’t make any errors
pcall will return false if the code does make any errors

local success, message = pcall(function()
nil:Destroy() --will not print any error
end)
1 Like

and preventing the script from stopping

I know some people who do. I don’t.