local rs = game:GetService('RunService')
local ReplicatedStorage = game:GetService("ReplicatedStorage")
local SimplePath = require(ReplicatedStorage.Modular_Scripts.SimplePath)
local animtrack = script.Parent.Humanoid:LoadAnimation(script.Parent.Grab)
local Debounce = {}
local zombie = script.Parent
local Path = SimplePath.new(zombie)
local AttackDistance = 2
rs.Heartbeat:Connect(function() -- Assign Charater Function
char = SimplePath.GetNearestCharacter(script.Parent.HumanoidRootPart.Position)
end)
function GoTarget() -- Pathfinding Function
task.wait(0.5)
if zombie.Humanoid.Health >= 1 then
Path.Visualize = true
Path:Run(char.HumanoidRootPart)
end
end
function ZombieHealthChanged() -- Zombie Health Detection
if zombie.Humanoid.Health > 0 then
-- Play Zombie Hurt Sound & Animation
print("Zombie Is Hurt")
else if zombie.Humanoid.Health == 0 then
Path:Destroy()
-- leaderstats.Cash.Value + 5
task.wait(10)
zombie:Destroy()
end
end
end
local function damageTarget() -- Hurt Player if zombie attacks
local magnitude = (char.HumanoidRootPart.Position - zombie.HumanoidRootPart.Position).Magnitude
if magnitude <= AttackDistance then
print("Zombie is hurting player")
char.Humanoid:TakeDamage(5)
end
end
----Zombie knows to compute path again if something blocks the path
Path.Blocked:Connect(function()
Path:Run(char.HumanoidRootPart)
end)
----If the position of Goal changes at the next waypoint, compute path again
Path.WaypointReached:Connect(function()
Path:Run(char.HumanoidRootPart)
end)
----Zombie knows to compute path again if an error occurs
Path.Error:Connect(function(errorType)
--Path:Run(char:GetPivot().Position)
Path:Run(char.HumanoidRootPart)
end)
---- Function Call
while true do task.wait()
GoTarget()
zombie.Humanoid.HealthChanged:Connect(ZombieHealthChanged)
damageTarget()
end
you try to find the target’s character every frame physics gets processed, however, the player’s character likely didn’t load by the time you run this script. Thus causing errors as you try to access humanoidrootpart when the char variable is nil(don’t exist).
local rs = game:GetService('RunService')
local ReplicatedStorage = game:GetService("ReplicatedStorage")
local SimplePath = require(ReplicatedStorage.Modular_Scripts.SimplePath)
local animtrack = script.Parent.Humanoid:LoadAnimation(script.Parent.Grab)
local Debounce = {}
local zombie = script.Parent
local Path = SimplePath.new(zombie)
Path.Visualize = true
local AttackDistance = 2
rs.Heartbeat:Connect(function() -- Assign Charater Function
char = SimplePath.GetNearestCharacter(script.Parent.HumanoidRootPart.Position)
end)
function GoTarget() -- Pathfinding Function
task.wait(0.5)
if char == nil then return end -- prevent the rest from executing if char don't exist
if zombie.Humanoid.Health >= 1 then
-- unnecessary, only need to set once Path.Visualize = true
Path:Run(char.HumanoidRootPart)
end
end
function ZombieHealthChanged() -- Zombie Health Detection
if zombie.Humanoid.Health > 0 then
-- Play Zombie Hurt Sound & Animation
print("Zombie Is Hurt")
else if zombie.Humanoid.Health == 0 then
Path:Destroy()
-- leaderstats.Cash.Value + 5
task.wait(10)
zombie:Destroy()
end
end
end
local function damageTarget() -- Hurt Player if zombie attacks
local magnitude = (char.HumanoidRootPart.Position - zombie.HumanoidRootPart.Position).Magnitude
if magnitude <= AttackDistance then
print("Zombie is hurting player")
char.Humanoid:TakeDamage(5)
end
end
----Zombie knows to compute path again if something blocks the path
Path.Blocked:Connect(function()
Path:Run(char.HumanoidRootPart)
end)
----If the position of Goal changes at the next waypoint, compute path again
Path.WaypointReached:Connect(function()
Path:Run(char.HumanoidRootPart)
end)
----Zombie knows to compute path again if an error occurs
Path.Error:Connect(function(errorType)
--Path:Run(char:GetPivot().Position)
Path:Run(char.HumanoidRootPart)
end)
zombie.Humanoid.HealthChanged:Connect(ZombieHealthChanged)
---- Function Call
while true do
-- unnecessary, you already wait 0.5 inside GoTarget: task.wait()
-- zombie.Humanoid.HealthChanged:Connect(ZombieHealthChanged) you are creating a brand new connection that triggers whenever zombie health changed every 0.5 seconds, only a single connection is needed
GoTarget()
if char == nil then return end -- prevent the rest from executing if char don't exist
damageTarget()
end
That code change caused the entire script to break instead of waiting until char was detected, I just put the zombie in server storage and spawn it after 15 seconds since it will be in a spawning system anyways.