01 | local Humanoid = script.Parent:FindFirstChild( "Humanoid" ) |
02 |
03 | function 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 |
13 | end |
14 |
15 | Humanoid.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.
Normally the Value of creator is a UserData (Instance). So you need to find the Character.
01 | local Humanoid = script.Parent:WaitForChild( "Humanoid" ) |
02 |
03 | Humanoid.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 |
17 | end ) |
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.
I'm assuming that 'hostile' is an ObjectValue
.
Try this:
01 | local Humanoid = script.Parent:FindFirstChild( "Humanoid" ) |
02 |
03 | function 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 |
13 | end |
14 |
15 | Humanoid.HealthChanged:Connect(findHostile) |