Hello, I am having this issue with my zombies:
robloxapp-20230901-2044448.wmv (2.5 MB)
The zombies are standing still for a short time after spawning in, it is related to the HumanoidRootPart being anchored during the spawning anim, but I need it to be anchored for the cool spawning effect. Any ways to fix other than make it so the HumanoidRootPart stays anchored?
Code:
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 = 5
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 and entity.HumanoidRootPart.Anchored == false 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
humanoid.Died:Connect(death)
RunService.Heartbeat:Connect(function()
if humanoid.Health <= 0 and canDie then
elseif entity.HumanoidRootPart.Anchored == false then
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
spawnAnim:Play()
spawnAnim.Ended:Wait()
entity.HumanoidRootPart.Anchored = false
forceField:Destroy()
end
end
entity.Changed:Connect(entitySpawned)