✓ Accepted answer
It's not that the code is so-called faulty, it's the fact that it reached the EOF and ceased execution. There is nothing yielding your program, therefore the conditional statement is evaluated upon runtime and then the program has nothing left to do; it finishes.
This is what we call a "semantic" error. This is the term used to refer to when a program runs properly, but not in the way we intended due to the nature of the logic structure. It all comes down to paying attention to this. For example, on line 02 you attempt to make a reference to the Health field, which is something you cannot do. The compiler is actually storing the of Healthinto hp at the time the instruction was acted upon; this will cause hp to be a static reflection.
Another way of looking at this.
Before helping you proceed with this, I recommend trying out the Attributes beta-feature here. With that let's resume; ensure you've added a numeric "Health" attribute.
You need to listen for when the Health has changed, then perform your evaluation on whether or not it has reached zero or lower. You can do that with the :GetAttributeChangedSignal() method of Instance. This will generate an RBXScriptSignal that is specifically tailored to react to the specified field's adjustment:
local Part = script.Parent
Part:GetAttributeChangedSignal("Health"):Connect(function()
if (Part:GetAttribute("Health") <= 0) then
Part:Destroy()
end
end)
The proper way to achieve what you wished
If you want to stick with using a Humanoid, then that is fine by me! You can actually listen for a signal by the name of HealthChanged. This will be similarly invoked when the Health property is adjusted:
local Part = script.Parent
local Humanoid = Part.pp
Humanoid.HealthChanged:Connect(function(NewHealth)
if (NewHealth <= 0) then
Part:Destroy()
end
end)
2 comments
Please read my new comment.
Please read my updates.