I tried to make a model and inside the model there is a humanoid, a part called ''head'' and a script. The health in the humanoid = 100 and the maxhealth = 100. The part called ''head'''s transparency = 0. And i have a sword wich can damage the part until its <= 0 health. So i tried to make the part called ''head'' invisible when it's health = 0 or lower. This is what was inside the script:
function myFunction() if script.Parent.Humanoid.Health <= 0 then script.Parent.Head.Transparency = 1 end end myFunction()
I just started scripting so i'm not the best but can anyone tell me what the problem here is? Thanks :D
script.Parent.Humanoid.HealthChanged:connect(function() if script.Parent.Humanoid.Health == 0 then script.Parent.Head.Transparency = 1 else script.Parent.Head.Transparency = 0 end end)
This script should work.
The problem with your script is that you call the function right after you define it once. when scripts get to the end of a script, they will stop functioning because there is no more code to execute, (unless there are events which i will talk about)
So essentially, in your script, as soon as the function is defined, it runs it once, and then it is done.
Your code was perfect except for that fact and the fact that the transparenxy wont change back.
So, in my solution, I used the "health changed" event of a humanoid. This avoids the problem with your code, as whenever the humanoid is hit, the code is executed.