local Humanoid = script.Parent:FindFirstChild("Humanoid") function findHostile() local hostile = Humanoid:FindFirstChild("creator") if hostile ~= nil then if hostile.Value ~= nil then local hostileHumanoid = hostile.Value:FindFirstChild("Humanoid") while hostileHumanoid.Health >= 0 do Humanoid:MoveTo(hostile.Position,hostile) end end end end 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.
local Humanoid = script.Parent:WaitForChild("Humanoid") Humanoid.HealthChanged:Connect(function() local creator = Humanoid:WaitForChild("creator") if creator ~= nil then local hostile = creator.Value print(hostile.Name, type(hostile), typeof(hostile)) if hostile ~= nil and hostile.Character then local hosCharacter = hostile.Character local hosHumanoid = hosCharacter:WaitForChild("Humanoid") local hosHPart = hosCharacter:WaitForChild("HumanoidRootPart") repeat wait() Humanoid:MoveTo(hosHPart.Position, hosHPart) until hosHumanoid.Health >= 0 or hosCharacter == nil end end 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:
local Humanoid = script.Parent:FindFirstChild("Humanoid") function findHostile() local hostile = Humanoid:FindFirstChild("creator") if hostile ~= nil then if hostile.Value ~= nil then local hostileHumanoid = hostile.Value while hostileHumanoid.Health >= 0 do Humanoid:MoveTo(hostile.Position,hostile) end end end end Humanoid.HealthChanged:Connect(findHostile)