I am having trouble with my zombie script again, when the zombie spawns in it plays an animation and when it finishes goes to normal activity. The problem occurs before it continues with it’s normal patrolling, for some reason there is a large delay, I don’t know what the problem is. Here is the entire AI script:
local pathfindingService = game:GetService("PathfindingService")
local RunService = game:GetService("RunService")
local TweenService = game:GetService("TweenService")
local DebrisService = game:GetService("Debris")
local entity = script.Parent
local humanoid = entity.Humanoid
local defaultWalkSpeed = humanoid.WalkSpeed
local sightDistance = 60
local attackRange = 6
local attackDelay = 1.5
local damage = 15
local attackAnim = humanoid:LoadAnimation(script.Attack)
local deathAnim = humanoid:LoadAnimation(script.Death)
local spawnAnim = humanoid:LoadAnimation(script.Spawn)
local canAttack = true
local canDie = true
entity.HumanoidRootPart:SetNetworkOwner(nil)
local function canSeeTarget(target)
local origin = entity.HumanoidRootPart.Position
local direction = (target.HumanoidRootPart.Position - entity.HumanoidRootPart.Position).unit * sightDistance
local ray = Ray.new(origin, direction)
local hit, pos = workspace:FindPartOnRay(ray, workspace.Zombies)
if hit then
if hit:IsDescendantOf(target) then
return true
else
return false
end
end
end
local function findTarget()
local players = game.Players:GetPlayers()
local maxDistance = sightDistance
local nearestTarget
for index, player in pairs(players) do
if player.Character then
local target = player.Character
local distance = (entity.HumanoidRootPart.Position - target.HumanoidRootPart.Position).Magnitude
if distance <= maxDistance and canSeeTarget(target) and target.Humanoid.Health > 0 then
nearestTarget = target
maxDistance = distance
end
end
end
return nearestTarget
end
local function getPath(destination)
local pathParams = {
["AgentHeight"] = 5,
["AgentRadius"] = 2.6,
["AgentCanJump"] = false
}
local path = pathfindingService:CreatePath(pathParams)
path:ComputeAsync(entity.HumanoidRootPart.Position, destination.Position)
return path
end
local function generateSound(folder)
local randomNumber = Random.new()
local possibleSounds = folder:GetChildren()
local randomSound = possibleSounds[randomNumber:NextInteger(1, #possibleSounds)]
local selectedSound = randomSound:Clone()
selectedSound.Parent = entity.HumanoidRootPart
return selectedSound
end
local function attack(target)
local distance = (entity.HumanoidRootPart.Position - target.HumanoidRootPart.Position).Magnitude
if distance <= attackRange and canAttack then
canAttack = false
attackAnim:Play()
attackAnim:GetMarkerReachedSignal("Attack"):Wait()
if (entity.HumanoidRootPart.Position - target.HumanoidRootPart.Position).Magnitude <= attackRange then
target.Humanoid.Health -= damage
end
attackAnim.Ended:Wait()
task.wait(attackDelay)
canAttack = true
end
end
local function patrol()
local target = findTarget()
local attackCo = coroutine.create(attack)
if target then
coroutine.resume(attackCo, target)
humanoid:MoveTo(target.HumanoidRootPart.Position)
end
end
local function death()
canDie = false
entity.HumanoidRootPart.Anchored = true
entity.HumanoidRootPart.Death:Play()
deathAnim:Play()
deathAnim:GetMarkerReachedSignal("Death"):Wait()
entity:Destroy()
end
RunService.Heartbeat:Connect(function()
if humanoid.Health <= 0 and canDie then
death()
else
patrol()
wait(0.01)
end
end)
local function entitySpawned(property)
if property == "Parent" then
local forceField = Instance.new("ForceField")
forceField.Visible = false
forceField.Parent = entity
entity.HumanoidRootPart.Anchored = true
canAttack = false
spawnAnim:Play()
spawnAnim.Ended:Wait()
entity.HumanoidRootPart.Anchored = false
canAttack = true
forceField:Destroy()
end
end
entity.Changed:Connect(entitySpawned)