Pcall doesn't work for a function in another module

I have a basic function inside a module script so I can use it in other scripts:

function Library:PartToBP(part, character)
	local map =
		{
			[character:FindFirstChild("Head")] = character.BodyParts.Head,
			[character:FindFirstChild("Left Arm")] = character.BodyParts.LArm,
			[character:FindFirstChild("Left Leg")] = character.BodyParts.LLeg,
			[character:FindFirstChild("Right Arm")] = character.BodyParts.RArm,
			[character:FindFirstChild("Right Leg")] = character.BodyParts.RLeg,
			[character:FindFirstChild("Torso")] = character.BodyParts.Torso
		}
	if map[part] then
		return map[part]
	end
end

Due to the possibility of bodyparts getting removed from thee character, some bodyparts stop existing, hence the use of FindFirstChild() to avoid errors. Of course, this is a terrible solution and I’ll have a dictionary of all nils in a worst case scenario. So I use pcall to see how it affects my code as seen here:

	local status, bodyPartValue = pcall(lib.PartToBP, bodyPart, character)
	if not status then
		print(bodyPartValue)
		return
	end

When I run this, I get a really strange error:
ReplicatedStorage.Library:52: attempt to index nil with 'FindFirstChild'

which is a reference to this line of code:

[character:FindFirstChild("Head")] = character.BodyParts.Head,

Which is literally impossible. I assume i’m using Pcall() incorrectly. But how?

Just to settle something, I also added a print(character) call to double check and I get this result:
image
So clearly character isn’t nil.

You realize you have a print in your code right?

pcall return 2 things.

The first is a boolean (true or false) which is true when your function is successful, and false if it is unsuccessful.

The second has two possibilities based on the booleans:

  1. It was successful, so you get the return value
  2. It wasn’t successful, so you get the error message

In your code, you’re checking if it wasn’t successful, and then printing the error message. I’m not sure what you wanted to do.

I moved thee print outside to check if the pcall was ever successful

	local status, bodyPartValue = pcall(lib.PartToBP, bodyPart, character)
	print(bodyPartValue, status)
	if not status then
		return
	end

pcall() always returns false with the same error. I don’t think the arguments are being passed into the function properly or at least that’s what I’m getting from the error.

First thing I can think of is that the function is a : method that passes itself as a hidden first variable, which offsets your arguments passed through.
Since the function by default incorporates a self first argument, the function is essentially:

function Library:PartToBP(part, character)
into
function Library.PartToBP(self, part, character)

And since you’re calling the function by . and only passing two arguments, you have self being bodyPart and part being character.

Now if you want to pcall without making another function to just call it with the : method, you just have to pass itself for the first argument, or just pass nil, as such:

...
pcall(lib.PartToBP, lib, bodyPart, character)

Alternatively, if you declared the function in the ModuleScript as a .Function method, you can skip over this whole scenario.