So I am making an enemy npc system, and everything but the animation works. So, the damaging works. (which is literally after the animation) It all goes to the right place, and there is no error. Here is my script:
local npc = script.Parent
local hrpOfNPC = npc:WaitForChild("HumanoidRootPart")
local attackBoundaries = workspace:WaitForChild("Areas"):WaitForChild("Level 7 - Thieves"):WaitForChild("AttackBoundaries")
local plrsHit = {}
local maxDistance = 35
local attackCooldown = 1
local canAttack = true
local damageSound = Instance.new("Sound")
damageSound.Parent = npc
damageSound.SoundId = "rbxassetid://159504677"
local damageAnimation = script:WaitForChild("Animation")
local animator = npc.Humanoid:FindFirstChildOfClass("Animator")
local damageAnimTrack = animator:LoadAnimation(damageAnimation)
npc.Humanoid.Touched:Connect(function(touch)
if not canAttack then return end
local player = game.Players:GetPlayerFromCharacter(touch.Parent)
if player and not plrsHit[player] then
plrsHit[player] = true
canAttack = false
local humanoid = touch.Parent:FindFirstChild("Humanoid")
if humanoid then
humanoid:TakeDamage(25)
damageSound:Play()
print("Playing damage animation")
damageAnimTrack:Play()
plrsHit[player] = false
end
task.wait(attackCooldown)
canAttack = true
end
end)
while wait() do
local plrs = game.Players:GetPlayers()
local closestHRP
for i, plr in pairs(plrs) do
if plr.Character and plr.Character:FindFirstChild("HumanoidRootPart") and plr.Character.Humanoid.Health > 0 then
local hrp = plr.Character.HumanoidRootPart
local distanceBetween = (hrpOfNPC.Position - hrp.Position).Magnitude
if not closestHRP then closestHRP = hrp end
if (hrpOfNPC.Position - closestHRP.Position).Magnitude > distanceBetween then
closestHRP = hrp
end
end
end
if closestHRP then
local npcPos = hrpOfNPC.Position
local targetPos = closestHRP.Position
local withinBoundaries = attackBoundaries.CFrame:PointToObjectSpace(targetPos).Magnitude <= attackBoundaries.Size.Magnitude / 2
if withinBoundaries and (npcPos - targetPos).Magnitude <= maxDistance then
npc.Humanoid:MoveTo(targetPos)
end
end
end
The EnemyAnimator is a LocalScript and should be placed in StarterPlayerScripts.
local ReplicatedStorage = game:GetService("ReplicatedStorage")
local npc = script.Parent
local hrpOfNPC = npc:WaitForChild("HumanoidRootPart")
local attackBoundaries = workspace:WaitForChild("Areas"):WaitForChild("Level 7 - Thieves"):WaitForChild("AttackBoundaries")
local plrsHit = {}
local maxDistance = 35
local attackCooldown = 1
local canAttack = true
local damageSound = Instance.new("Sound")
damageSound.Parent = npc
damageSound.SoundId = "rbxassetid://159504677"
local damageAnimation = script:WaitForChild("Animation")
local animator = npc.Humanoid:FindFirstChildOfClass("Animator")
local damageAnimTrack = animator:LoadAnimation(damageAnimation)
local AnimateEvent: RemoteEvent = ReplicatedStorage:WaitForChild("Events"):WaitForChild("EnemyAnimateEvent")
npc.Humanoid.Touched:Connect(function(touch)
if not canAttack then return end
local player = game.Players:GetPlayerFromCharacter(touch.Parent)
if player and not plrsHit[player] then
plrsHit[player] = true
canAttack = false
local humanoid = touch.Parent:FindFirstChild("Humanoid")
if humanoid then
humanoid:TakeDamage(25)
damageSound:Play()
print("Playing damage animation")
--damageAnimTrack:Play()
AnimateEvent:FireAllClients(humanoid, damageAnimation) -- sends a message to all connected clients with the humanoid and animation. this will be used to play the animation on the client.
plrsHit[player] = false
end
task.wait(attackCooldown)
canAttack = true
end
end)
--[...]
-- EnemyAnimator.lua
local ReplicatedStorage = game:GetService("ReplicatedStorage")
local AniamteEvent: RemoteEvent = ReplicatedStorage:WaitForChild("Events"):WaitForChild("EnemyAnimateEvent")
AniamteEvent.OnClientEvent:Connect(function(humanoid: Humanoid, animation: Animation) --once we receive the humanoid and animation from the server, we load the animation and play it
local animator = humanoid:FindFirstChildOfClass("Animator")
animator:LoadAnimation(animation):Play()
end)