I’ve only started taking interest in the pcall() function, and I’m confused about how I would use it? I’ve set up a pcall() in a health bar tween function, yet even though I check for the error, the error still gets printed and stops the script. Even if there is a better way for me to handle the tweening and health bar, I’d still like to understand how I should use pcall() as I have other uses for it. I assume I am using it correctly? If not, could someone explain it’s use.
local succ,err = pcall(function()
Char:WaitForChild("Humanoid"):GetPropertyChangedSignal("Health"):Connect(function()
local Bar = newTag.NameHolder.HealthHolder.Bar
local Humanoid = Char.Humanoid
Bar:TweenSize(UDim2.new(Humanoid.Health/Humanoid.MaxHealth,0,1,0),"Out","Sine",0.4,true)
if Humanoid.Health >= HealthRanges.Green.Min and Humanoid.Health <= HealthRanges.Green.Max then
TweenService:Create(Bar,TweenInfo.new(0.5),{BackgroundColor3 = HealthColors.Green}):Play()
elseif Humanoid.Health >= HealthRanges.Yellow.Min and Humanoid.Health <= HealthRanges.Yellow.Max then
TweenService:Create(Bar,TweenInfo.new(0.5),{BackgroundColor3 = HealthColors.Yellow}):Play()
elseif Humanoid.Health >= HealthRanges.Red.Min and Humanoid.Health <= HealthRanges.Red.Max then
TweenService:Create(Bar,TweenInfo.new(0.5),{BackgroundColor3 = HealthColors.Red}):Play()
end
end)
end)
if succ then
print("Tweened health for; "..Player.Name)
elseif not succ and err then
print(err)
end
Line 5 is where the error happens if the player falls out of the world. This is the error;
'Can only tween objects in the workspace'
Shouldn’t the error be printed at ‘print(err)’?
Thanks.