Should I use one pcall() or two?

Just a simple question
Screenshot 2022-09-27 165704

only one pcall() is required since their just 2 separate if statements

1 Like

I would use two. If you combine them in one pcall, then the first gamepass check could fail, causing the second to never be checked.

After that I’d likely set up a loop to attempt to check gamepass ownership later on until it succeeds.

1 Like

Using 2 pcall() statements is the best solution as stated by @Alkan.

Lets say you have one pcall() check for both:

local Success,ErrorMessage = pcall(function() -- Protection!
   if MS:UserOwnsGamePassAsync(Player.UserId,Passes.VIP) then -- Will stop here if there is an error
      SetVIPData(Player)
   end
   if MS:UserOwnsGamePassAsync(Player.UserId,Passes.ObsidianPack) then -- Will stop here if there is an error
      GiveObsidianPackItems(Player)
   end
end)
if not Success then
   warn(ErrorMessage) -- VIP or Obsidian had an error!
end

If the VIP one fails, it will skip the Obsidian Pack one as well since it will stop upon error. However, using a second one allows both to be checked, even if one has an error:

local VIPSuccess,VIPErrorMessage = pcall(function() -- Protection!
   if MS:UserOwnsGamePassAsync(Player.UserId,Passes.VIP) then -- Will stop here if there is an error
      SetVIPData(Player)
   end
end)
if not VIPSuccess then
   warn(VIPErrorMessage) -- VIP had an error!
end
local ObsidianSuccess,ObsidianErrorMessage = pcall(function() -- Protection!
   if MS:UserOwnsGamePassAsync(Player.UserId,Passes.ObsidianPack) then -- Will stop here if there is an error
      GiveObsidianPackItems(Player)
   end
end)
if not ObsidianSuccess then
   warn(ObsidianErrorMessage) -- Obsidian had an error!
end

You can then use this to make it easier to loop for a certain amount of tries to ensure that the user does get each of the gamepasses, even if one fails. It won’t have to check again and risk another error (which is unlikely, but still possible). Always good to use proper scripting protection for each statement individually.

Edit: Happy this was the solution! I use this all the time and it helps a ton.

2 Likes