Pcall doesn't catch failed type coercion

Hey, I have a simple question. Does pcall not catch type coercion for roblox instances?

I have a script set up like this:

local success, commandSuccess, commandInfo = pcall(function()

    -- handle command failure

    targetHumanoid[targetProperty] = targetValue

    return true
end)

Which does catch the not a valid member error, but does not catch the failed type coersion of targetValue.

I know how to handle this separately, but is there a way to make pcall work nicely for this on its own?

pcall stands for protected call, it specifically protects against errors breaking your script (a high level explanation).

Type coercion is specifically through the linter and typing system and are not actually checked at runtime. This means any type issues in your code will result in uncatchable errors. Pcall will look out for errors thrown from the function itself, which your type coercion is not caught because it is not an error from the function, but an error from the typing system.

Secondly “not a valid member” seems to come from the targetProperty not being a valid member of targetHumanoid, meaning the property you’re passing is not a correct property for the humanoid. make sure that what you’re passing is a valid member of humanoid. i.e Humanoid.health is not valid, but Humanoid.Health is.

2 Likes

Thank you for explaining why pcall won’t catch failed type coercion. I will be sure to handle it properly with that in mind.

Yes, I did understand where that error was coming from. I was confused why it caught that error but not the type coercion error, which you explained. Thank you again!

1 Like