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

Why does my script index a nil value from Humanoid.Health?

Asked by 5 years ago
01local Humanoid = script.Parent:FindFirstChild("Humanoid")
02 
03function findHostile()
04    local hostile = Humanoid:FindFirstChild("creator")
05    if hostile ~= nil then
06        if hostile.Value ~= nil then
07            local hostileHumanoid = hostile.Value:FindFirstChild("Humanoid")
08            while hostileHumanoid.Health >= 0 do
09                Humanoid:MoveTo(hostile.Position,hostile)
10            end
11        end
12    end
13end
14 
15Humanoid.HealthChanged:Connect(findHostile)

It said that 'while hostileHumanoid.Health >= 0 do' tried to index a nil value with Health. Could anyone help me? This is a script under an NPC, and I'm trying to make it so that once you damage it, it follows you instantly.

3 answers

Log in to vote
1
Answered by 5 years ago
Edited 5 years ago

Normally the Value of creator is a UserData (Instance). So you need to find the Character.

01local Humanoid = script.Parent:WaitForChild("Humanoid")
02 
03Humanoid.HealthChanged:Connect(function()
04    local creator = Humanoid:WaitForChild("creator")
05    if creator ~= nil then
06        local hostile = creator.Value
07        print(hostile.Name, type(hostile), typeof(hostile))
08        if hostile ~= nil and hostile.Character then
09            local hosCharacter = hostile.Character
10            local hosHumanoid = hosCharacter:WaitForChild("Humanoid")
11            local hosHPart = hosCharacter:WaitForChild("HumanoidRootPart")
12            repeat wait()
13                Humanoid:MoveTo(hosHPart.Position, hosHPart)
14            until hosHumanoid.Health >= 0 or hosCharacter == nil
15        end
16    end
17end)
0
Thanks! CrypxticDoge 135 — 5y
Ad
Log in to vote
1
Answered by 5 years ago

Try changing

while hostileHumanoid.Health >= 0 do

to

while hostileHumanoid and hostileHumanoid.Health >= 0 do

Since you do :FindFirstChild'Humanoid' you need to check if it didnt return nil first.

0
Sorry doesnt work CrypxticDoge 135 — 5y
0
It seems hostileHumanoid is nil itself CrypxticDoge 135 — 5y
Log in to vote
0
Answered by
qVoided 221 Moderation Voter
5 years ago

I'm assuming that 'hostile' is an ObjectValue.

Try this:

01local Humanoid = script.Parent:FindFirstChild("Humanoid")
02 
03function findHostile()
04    local hostile = Humanoid:FindFirstChild("creator")
05    if hostile ~= nil then
06        if hostile.Value ~= nil then
07            local hostileHumanoid = hostile.Value
08            while hostileHumanoid.Health >= 0 do
09                Humanoid:MoveTo(hostile.Position,hostile)
10            end
11        end
12    end
13end
14 
15Humanoid.HealthChanged:Connect(findHostile)
1
Remeber that health is not a child of a Player CrypxticDoge 135 — 5y
0
Im gonna edit this, wait qVoided 221 — 5y
0
What is the hostileHumanoid's value? qVoided 221 — 5y
0
its supposed to be the humanoid of the player that attacked the NPC CrypxticDoge 135 — 5y

Answer this question