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

Simple If Statement not working? What am I doing wrong?

Asked by 4 years ago
Edited 4 years ago

I have an NPC Monster that has a Humanoid named "Human" and it is supposed to be destroyed after its health reaches 0 or lower. It is a Server Script and is located inside the head of the Monster.

This is the code:

if script.Parent.Parent:FindFirstChild("Human").Health <= 0 then
    script.Parent.Parent:Destroy()
    print "Monster has died"
end

It does not print "Monster has died" and the only reason I can think of it not working is because the "Monster" for now is just a wall named "Head" and chases the player with a chasing script.

I'm a new scripter so I might have done something very stupid but I really can't figure this out :/

0
is your script disabled? 123nabilben123 499 — 4y
0
nope RoblotXRB 3 — 4y

2 answers

Log in to vote
1
Answered by 4 years ago

First of all, your print doesn't have the brackets on line 3.

Secondly, health is not found in a thing called Human. It's a Humanoid.

So, here would be the fixed script:

if script.Parent.Parent:FindFirstChild('Humanoid').Health <= 0 then
    script.Parent.Parent:Destroy()
    print("Monster has died")
end

Hope I helped!

0
Thank you for your response. However, it still does not seem to work. It doesn't print or get destroyed. Also I named the Humanoid "Human" because when I tried yours it said I'm indexing a nil value in the Output. RoblotXRB 3 — 4y
0
On line one, try fixing it to "script.Parent" not "script.Parent.Parent". Also, try replacing the ':FindFirstChild()' with ':WaitForChild()' Sensei_Developer 298 — 4y
0
Still nothing. No error in the Output. Nothing printed. :/ RoblotXRB 3 — 4y
0
Hm. That's strange... Sensei_Developer 298 — 4y
View all comments (2 more)
0
Humanoid.Died event does not work either :( RoblotXRB 3 — 4y
0
Does it have something to do with the fact it's simply a wall with health named "Head" and no other parts such as Torso and it's Limbs? RoblotXRB 3 — 4y
Ad
Log in to vote
0
Answered by 4 years ago

Figured it out! The code should be:

script.Parent.Human.HealthChanged:Connect(function()
    if script.Parent:FindFirstChild("Human").Health <= 0 then
        script.Parent:Destroy()
        wait()
    end
end)

It is because my first code simply checks the health at the beginning and sees it's at full health and ignores it afterwards. However, this one checks whenever its health changes and when it finally changes to 0 or under, it is destroyed.

0
Good job, buddy! Sensei_Developer 298 — 4y

Answer this question