Scripting Helpers is winding down operations and is now read-only. More info→
Ad
Log in to vote
0

How do I Make a part destroy after reaching 0 health?

Asked by 3 years ago
Edited 3 years ago

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.

0
Please read my new comment. Ziffixture 6913 — 3y
0
Please read my updates. Ziffixture 6913 — 3y

3 answers

Log in to vote
2
Answered by
Ziffixture 6913 Moderation Voter Community Moderator
3 years ago
Edited 3 years ago

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:

--###----------[[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)
2
attributes are actually no longer a beta, they were released 18 days ago https://devforum.roblox.com/t/attributes-now-available/1066934 imKirda 4491 — 3y
0
question, would this mean put a event in replicated storage and name it Died with the second piece of code that's there? oXDestroyerXo 28 — 3y
0
Read the documentation of RBXScriptSignal; no, you do not have to. Ziffixture 6913 — 3y
0
If this works, don't forget to accept the answer. Thanks for the head's up, imKirda! Ziffixture 6913 — 3y
View all comments (9 more)
0
Will do ;) oXDestroyerXo 28 — 3y
0
I've tried multiple ways to get the code to work I'll send you a link to a Youtube video showing how I tried incase I am doing this wrong, sorry if I'm being a little too persistent. :) oXDestroyerXo 28 — 3y
0
https://youtu.be/jsTKASKpFes alright I put a lot of effort into this video that took 40 minutes to make. oXDestroyerXo 28 — 3y
0
Your tools were programmed to search for the Humanoid Instance. You need to reprogram them to modify the Health attribute instead, or, simply apply the second method I provided. The reasoning behind why the program did not act upon your manual adjustment of the field, is due to the fact that is was locally modified; FE regulations inhibit the Client from making unjust changes on the Server. Ziffixture 6913 — 3y
0
(Switch to the Server's view to make the warranted change) Ziffixture 6913 — 3y
0
ok so after switching to the server view and making the change to the numeric attribute/humanoid in both instances the part would still be there when I run the game. oXDestroyerXo 28 — 3y
0
Using my Health attribute code: https://gyazo.com/9ea85b281182be39d6c5f30c3839495a Ziffixture 6913 — 3y
0
I forgot that the Died signal is tailored to Character Rigs, so I made the proper adjustment and it works as well! Ziffixture 6913 — 3y
0
Ive learned a bit more about scripting since this oXDestroyerXo 28 — 2y
Ad
Log in to vote
0
Answered by 3 years ago

I've watched the video, would you mind turn on team create so I can help you? Username: piebiedieroti

0
I mean sure why not all I have is a brick with a sloppy script in it and a humanoid oXDestroyerXo 28 — 3y
Log in to vote
0
Answered by 3 years ago

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!

Answer this question