Hi, So i am making a zombie “Ai” for a game of mine and for some reason currently the zombie is moving when idle? and frozen when walking (animations) which is really strange
I have tried some fixes but none of them worked out
Btw if your asking what the “Idle” bool does it does nothing cause that didn’t work and i am lazy to remove it ![]()
Sorry if i made any grammar mistakes ![]()
-- // Services & Variables
local players = game:GetService("Players")
local runService = game:GetService("RunService")
local pathfindingService = game:GetService("PathfindingService")
local zombie = script.Parent
local humanoid = zombie:WaitForChild("Humanoid")
local hrp = zombie:WaitForChild("HumanoidRootPart")
-- Settings
local attackRange = 4
local runRange = 35
local walkSpeed = 6
local runSpeed = 25
local lastAttack = 0
local attackCooldown = 1.5 -- seconds
local idle = true
-- Animations
local animator = humanoid:WaitForChild("Animator")
local anims = {
idle = animator:LoadAnimation(script.Anims.Idle),
walk = animator:LoadAnimation(script.Anims.Walk),
run = animator:LoadAnimation(script.Anims.Run),
--attack = animator:LoadAnimation(script.Anims.Attack)
}
local currentAnim = nil
local function playAnim(newAnim)
if currentAnim == newAnim then return end
if currentAnim and currentAnim.IsPlaying then
currentAnim:Stop()
end
newAnim:Play()
currentAnim = newAnim
end
-- get nearest player
local function getNearestPlayer()
local nearest, dist = nil, math.huge
for _, player in ipairs(players:GetPlayers()) do
if player.Character and player.Character:FindFirstChild("HumanoidRootPart") then
local mag = (player.Character.HumanoidRootPart.Position - hrp.Position).Magnitude
if mag < dist then
dist = mag
nearest = player
end
end
end
return nearest, dist
end
local currentPath = nil
local pathWaypoints = {}
local currentWaypointIndex = 1
local function moveToPosition(targetPosition)
local path = pathfindingService:CreatePath({
AgentRadius = 2,
AgentHeight = 5,
AgentCanJump = true,
WaypointSpacing = 4
})
local success, errorMessage = pcall(function()
path:ComputeAsync(hrp.Position, targetPosition)
end)
if success and path.Status == Enum.PathStatus.Success then
currentPath = path
pathWaypoints = path:GetWaypoints()
currentWaypointIndex = 1
if #pathWaypoints > 1 then
humanoid:MoveTo(pathWaypoints[2].Position)
currentWaypointIndex = 2
end
else
humanoid:MoveTo(targetPosition)
end
end
humanoid.MoveToFinished:Connect(function(reached)
if currentPath and pathWaypoints and currentWaypointIndex then
currentWaypointIndex = currentWaypointIndex + 1
if currentWaypointIndex <= #pathWaypoints then
humanoid:MoveTo(pathWaypoints[currentWaypointIndex].Position)
end
end
end)
humanoid.Died:Connect(function()
task.wait(5)
zombie:Destroy()
end)
local canMove = true
local isMoving = false
local function moveToRandomNearTarget(targetHRP)
if not canMove or isMoving then return end
canMove = false
isMoving = true
if math.random() < 0.3 then
playAnim(anims.idle)
idle = true
task.wait(math.random(1, 2))
else
local offset = Vector3.new(math.random(-6, 6), 0, math.random(-6, 6))
local goalPos = targetHRP.Position + offset
moveToPosition(goalPos)
idle = false
playAnim(anims.walk)
task.wait(0.5)
end
isMoving = false
task.wait(0.5)
canMove = true
end
-- main behavior loop
task.spawn(function()
while task.wait(0.1) do
local target, dist = getNearestPlayer()
if target and target.Character and target.Character:FindFirstChild("Humanoid") then
local targetHRP = target.Character.HumanoidRootPart
local targetHumanoid = target.Character.Humanoid
if targetHumanoid.Health > 0 then
if dist <= attackRange then
humanoid:MoveTo(hrp.Position)
idle = true
playAnim(anims.idle)
if tick() - lastAttack >= attackCooldown then
lastAttack = tick()
--playAnim(anims.attack)
targetHumanoid:TakeDamage(20)
end
else
if dist < runRange then
humanoid.WalkSpeed = runSpeed
moveToPosition(targetHRP.Position)
idle = false
playAnim(anims.run)
else
humanoid.WalkSpeed = walkSpeed
moveToRandomNearTarget(targetHRP)
end
end
else
humanoid:MoveTo(hrp.Position)
idle = true
playAnim(anims.idle)
end
else
humanoid:MoveTo(hrp.Position)
idle = true
playAnim(anims.idle)
end
end
end)
Thank you! ![]()