I tried to make a part with a humanoid named pp and a script, after making those two I added
local p = script.Parent local h = p:FindFirstChild("pp") local hp = h.Health if hp <= 1 then p:Destroy() end
Into the script, this didn't work and I'm wondering why because I'm not a very experienced scripter and I'm seeking help not the actual answer, the only thing that I can see I most likely got wrong is local h = p:FindFirstChild("pp") because I have no clue If I should have used ("pp") in that statement or ('pp') both end with the same outcome of the script not destroying the part after I hit it a few times with a sword. I've been working on this for a very long time and I think the whole script is wrong.
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 Health
into 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:
--###----------[[VARIABLES]]----------###-- local Part = script.Parent --###----------[[SETUP]]----------###-- 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:
--###----------[[VARIABLES]]----------###-- local Part = script.Parent local Humanoid = Part.pp --// An odd choice? --###----------[[SETUP]]----------###-- Humanoid.HealthChanged:Connect(function(NewHealth) if (NewHealth <= 0) then Part:Destroy() end end)
I've watched the video, would you mind turn on team create so I can help you? Username: piebiedieroti
Here is the thing
"if" statements only run once when called and what's happening here is that the "if" runs once while the Entity has more than once health so you can use the "Changed" event like this
-- Here is the script h.Changed:Connect(function() if hp == 0 then p:Destroy() end end)
I hope this helped!