Pcall error handling doesn't work

for some reason, pcall doesn’t work, and its needed for the error handler that i have

function Weave:Start(): Promise<any>
  return Promise.new(function(_, reject)
    started = true

    for name, mod in loaded do
		  local method = mod["OnStart"]
		  if type(method) == "function" then
        task.spawn(function()
			    local suc, er = pcall(function()
            debug.setmemorycategory(name)
				    method(mod)
			    end)
          if not suc then
            reject(er)
          end
        end)
		  end
	  end
  end)
end

when running, it still shows a normal error (ignore the actual error, i have made it on purpose just to test the handler, and it doesn’t work)

As for the :Start function, it works as shown:

return {OnStart = function()
   for _, plr in Players:GetPlayers() do
      task.spawn(PlayerAdded, plr)
   end

   Players.PlayerAdded:Connect(PlayerAdded)

   Players.PlayerRemoving:Connect(function(plr)
      local profile = Profiles[plr]
      if profile ~= nil then
         profile:EndSession()
      end
   end)
end}

I have already disabled debugger enabled, and tried a lot of different things, like not creating a new thread, creating the function inside the pcall (pcall(method, mod)) and it still shows as successful while returning an error

Which line is line 30 in the Datastore script?

Are you 100% sure that it is being called through the method call in Weave:Start?
(confirm this with print statements)

as i said, i had intentionally created an error there just to test the handler, but its part of the PlayerAdded() function

and im definitely sure that its called in Weave:Start() as theres only 1 script that isn’t a modulescript, and its calling that after requiring some modulescripts and placing them in the loaded variable
and i have also used print statements

I’ve done a bit of testing with your code, and have replicated the issue.

I have a suspicion of what is causing this problem, however I’ll need to properly test it before having conclusive results

Alright, I’ve done testing and gotten the error down to as simple a form as I could:

pcall(function()
	game["Run Service"].Stepped:Once(function() 
		error('testing')	
	end)
	error("test")
end)

the ‘testing’ error still occurs, while the ‘test’ error is caught.

Basically, it seems that pcall does not extend to connections (and their associated functions) within its scope. So, in your code, the error occurs in the PlayerAdded connection and hence bypasses the pcall, causing the behaviour you see.

1 Like

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.