Hi!
I’m having problems showing animations in my NPCs despite of trying several solutions taken from this forum. Please, let me detail my problem to see what I am missing.
I have created a very simple “hitting with a bat” animation for my player. It’s included inside a Bat tool and that Bat is inside my StarterPack. Each time the user clicks, the animation is played. This is the local script:
local bat = script.Parent
local replicatedStorage = game:GetService("ReplicatedStorage")
local hitEvent = replicatedStorage:FindFirstChild("hitEvent")
local players = game:GetService("Players")
local player = players.LocalPlayer
local mouse = player:GetMouse()
local debouncedMouse = true
local debouncedTouch = true
bat.Wood.Touched:Connect(function(hit)
local character = hit.Parent
local humanoid = character:FindFirstChild("Humanoid")
if humanoid and debouncedTouch then
debouncedTouch = false
hitEvent:FireServer(humanoid)
task.wait(1)
debouncedTouch = true
end
end)
mouse.Button1Down:Connect(function()
if debouncedMouse then
debouncedMouse = false
local animation = bat.battingAnimation
local humanoid = bat.Parent:FindFirstChild("Humanoid")
if humanoid then
local animationTrack = humanoid:LoadAnimation(animation)
animationTrack.Priority = Enum.AnimationPriority.Action
animationTrack.Looped = false
animationTrack:Play()
task.wait(1)
animationTrack:Stop()
animationTrack:Destroy()
end
debouncedMouse = true
end
end)
This is working OK. Each time I click with my mouse, my player plays the animation, so perfect.
Now I would like my NPCs to be equipped with this bat, pursue the players making the hitting animation looped and walking animation. To do so I’ve made this:
- Copy/Paste the Bat from StarterPack into my NPCs into the workspace.
- Remove the local script inside the NPC.Bat.
- Create a new server script inside the NPC.Bat with this content:
local bat = script.Parent
local debounced = true
bat.Wood.Touched:Connect(function(hit)
local character = hit.Parent
local humanoid = character:FindFirstChild("Humanoid")
if humanoid and debounced then
debounced = false
humanoid:TakeDamage(20)
task.wait(1)
debounced = true
end
end)
- Create a server script inside my NPC to pursue the nearest player with this content:
local npc = script.Parent
local pathfindingService = game:GetService("PathfindingService")
local npcHumanoid = npc:WaitForChild("Humanoid")
local npcRootPart = npc:WaitForChild("HumanoidRootPart")
local playersService = game:GetService("Players")
local MAX_DISTANCE = 25
local path = pathfindingService:CreatePath({
AgentCanJump = false
})
function getNearestPlayer()
local minDistance = math.huge
local nearestPlayer
local players = playersService:GetPlayers()
for index, player in ipairs(players) do
local character = player.Character or player.CharacterAdded:Wait()
local humanoidRootPart = character:WaitForChild("HumanoidRootPart")
local distance = (humanoidRootPart.Position - npcRootPart.Position).magnitude
if distance < minDistance then
minDistance = distance
nearestPlayer = player
end
end
if minDistance < MAX_DISTANCE then
return nearestPlayer
end
end
function animateNpc()
local animation = npc.Bat.battingAnimation
local humanoid = npc.Humanoid
if humanoid and animation then
local animationTrack = humanoid:LoadAnimation(animation)
animationTrack.Priority = Enum.AnimationPriority.Movement
animationTrack.Looped = true
animationTrack:Play()
task.wait(1)
animationTrack:Stop()
animationTrack:Destroy()
end
end
function goTowardsPlayer(player)
local character = player.Character or player.CharacterAdded:Wait()
local humanoidRootPart = character:WaitForChild("HumanoidRootPart")
local startingPoint = npcRootPart.Position
local endingPoint = humanoidRootPart.Position
path:ComputeAsync(startingPoint, endingPoint)
local waypoints = path:GetWaypoints()
for i, waypoint in pairs(waypoints) do
npcHumanoid:MoveTo(waypoint.Position)
npcHumanoid.MoveToFinished:Wait(1)
end
end
while task.wait(1) do
local nearestPlayer = getNearestPlayer()
if nearestPlayer then
animateNpc()
goTowardsPlayer(nearestPlayer)
end
end
With these changes, the NPC pursues the nearest player and the bat makes damage, so this is working OK (in fact, I’ve put lots of logs and they are all shown without problems and without any errors).
But, the NPC doesn’t play any animation, it slides instead of walking and it’s not hitting.
Regarding to the walking animation I’ve tried to use the Animate script that players have when they are playing copied inside the npc.humanoid but without success.
Regarding to the hitting animation I’ve tried to do use
local animator = humanoid:WaitForChild("Animator")
local animationTrack = animator:LoadAnimation(animation)
instead of
local animationTrack = humanoid:LoadAnimation(animation)
but I get this warning “Infinite yield possible on ‘Workspace.Adolescente cabreado.Humanoid:WaitForChild(“Animator”)’” and the animation is not played.
I’ve tried too using animationTrack.Priority = Enum.AnimationPriority.Movement and animationTrack.Priority = Enum.AnimationPriority.Action, removing the animation Stop and Destroy, removing the Looped=true, but always without success.
What could I try? Am I missing something?