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'
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.