i cant seem to figure out why the npc lags the game whenever it attacks
local humanoid = Zombie.Zombie
local animator = humanoid:WaitForChild("Animator",5)
local attackanim = animator:LoadAnimation(script.Attack)
local sounds = script.Parent.Head
local function attack(plr)
Zombie.Zombie:MoveTo(plr.HumanoidRootPart.Position)
script.Parent.HumanoidRootPart.Touched:Connect(function(hit)
if hit.Parent:FindFirstChild("Humanoid") and debounce == false then
if hit.Parent.Humanoid.Health > 0 then
debounce = true
Zombie.Zombie.WalkSpeed = 0.001
attackanim:Play()
sounds.S_Attack:Play()
hit.Parent.Humanoid:TakeDamage(20)
wait(0.3)
Zombie.Zombie.WalkSpeed = 20
wait(1)
debounce = false
end
end
end)
end
local canDie = true
local reward = 50
while wait() do
path:ComputeAsync(Zombie.HumanoidRootPart.Position, Vector3.new(math.random(-100,200), 0, math.random(-100,200)))
waypoints = path:GetWaypoints()
for i, waypoint in pairs(waypoints) do
local targetplr = findTarget()
if targetplr then
attack(targetplr)
else
Zombie.Zombie:MoveTo(waypoint.Position)
Zombie.Zombie.MoveToFinished:Wait()
end
if Zombie.Zombie.Health <= 0 then
local tag = script.Parent.Zombie:FindFirstChild("creator")
local msg = script.TextLabel
local killed = true
local txt = [[<font color="rgb(170, 72, 72)">Killed: </font>]]
msg.Visible = false
if tag and canDie then
canDie = false
local player = tag.Value
local leader = game.ServerStorage.PlayerCash:FindFirstChild(player.Name)
print(leader)
leader.Value = leader.Value + reward
end
msg.Visible = true
if killed then
msg.Text = txt..Zombie.Zombie.Parent.Name.." +$"..reward
end
msg.Parent = tag.Value:FindFirstChild("PlayerGui").KillNotification.Frame
wait(5)
script:Destroy()
end
end
end