Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
✓ ANSWER ACCEPTED

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

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.

2 comments

0

Please read my new comment.

0

Please read my updates.

3 answers

✓ 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:

--###----------[[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)

13 comments

2

attributes are actually no longer a beta, they were released 18 days ago https://devforum.roblox.com/t/attributes-now-available/1066934

0

question, would this mean put a event in replicated storage and name it Died with the second piece of code that's there?

0

Read the documentation of RBXScriptSignal; no, you do not have to.

0

If this works, don't forget to accept the answer. Thanks for the head's up, imKirda!

0

Will do ;)

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. :)

0

https://youtu.be/jsTKASKpFes alright I put a lot of effort into this video that took 40 minutes to make.

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.

0

(Switch to the Server's view to make the warranted change)

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.

0
0

I forgot that the Died signal is tailored to Character Rigs, so I made the proper adjustment and it works as well!

0

Ive learned a bit more about scripting since this

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!

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

1 comment

0

I mean sure why not all I have is a brick with a sloppy script in it and a humanoid

This discussion is preserved as it was. New answers, comments, and votes are closed.