I got No idea on how could I make the enemy play Animations

I’m making on Game named: Punching simulator, And In the past Day I’ve been struggling to make the Punch enemy script:

  1. I wan’t to make an script where whenever you punch enemy you get 1 coins and he gets damaged, and when his health reaches 0 he dies.

  2. The Issue is I don’t know how to make Animations for the Enemy whenever he gets damaged or death, I have no idea how could I make enemy play animations.

  3. I tried making "local EnemyHum = v.Parent:WaitForChild(“Humanoid”) and “EnemyHum.Damaged:Play()”.

But Even when I tried hard, It doesn’t work.

This is the script:

-- --Services
local player = game:GetService("Players").LocalPlayer
local WS = game:GetService("Workspace")
local ServerStorage = game:GetService("ServerStorage")

--Variables
local Mouse = player:GetMouse()
local Punch = ServerStorage:WaitForChild("Assets"):WaitForChild("Animation"):WaitForChild("Punch")
local Death = ServerStorage:WaitForChild("Assets"):WaitForChild("Animation"):WaitForChild("Death")
local Damaged = ServerStorage:WaitForChild("Assets"):WaitForChild("Animation"):WaitForChild("Damaged")

--functions
Mouse.Button1Down:Connect(function()
	local HitPart = Mouse.Target
    if HitPart then
        if HitPart.Parent.Parent.Parent:IsA("Folder") and HitPart.Parent.Parent.Parent.Name == "EnemyFolder" then
            for _,v in pairs(HitPart.Parent:GetChildren()) do
				if v:IsA("BasePart") and v.Parent.Parent.Values.IsEnemy.Value == true then --if it has a value called IsEnemy that is true deal damage to it.
					player:WaitForChild("leaderstats"):WaitForChild("Coins").Value += 1
					Punch:Play()
					local EnemyHum = v.Parent:WaitForChild("Humanoid")
					v.Parent.Parent.Values.Health -= 1
					EnemyHum.Damaged:Play()
					if v.Parent.Parent.Values.Health == 0 then
						EnemyHum.Death:Play()
						player:WaitForChild("leaderstats"):WaitForChild("Coins").Value += 20
						return
					end
                end
            end
        end 
    end 
end)

Please If you got any idea on how could i fix this, Please say it, Thanks!

What you can do is simply using a Humanoid.HealthChanged:Connect(function() and after firing the function, simply find if the health is lowered. (Sorry if my simple explanation is a bit sloppy)

1 Like

Do you set AnimationTrack? I see nothing and directly using “:Play()”

1 Like

Thank you but, actually the npc doesn’t have any health like its an simulator and i don’t wan’t to make npc when he dies to actually break joints apart so i made it only play animations, the health is just an intvalue i put inside of Values inside of the enemy

Ok so i need to use animationTrack and then do enemy to plays the animationtrack?

Can’t you just simply run a function where each time the IntValue changes negatively (3,2,1,0,-1) you play the animation?

1 Like

I changed the script to this

–Services
local player = game:GetService(“Players”).LocalPlayer
local WS = game:GetService(“Workspace”)
local ServerStorage = game:GetService(“ServerStorage”)

–Variables
local Mouse = player:GetMouse()
local Punch = ServerStorage:WaitForChild(“Assets”):WaitForChild(“Animation”):WaitForChild(“Punch”)
local Death = ServerStorage:WaitForChild(“Assets”):WaitForChild(“Animation”):WaitForChild(“Death”)
local Damaged = ServerStorage:WaitForChild(“Assets”):WaitForChild(“Animation”):WaitForChild(“Damaged”)

–functions
Mouse.Button1Down:Connect(function()
local HitPart = Mouse.Target
if HitPart then
if HitPart.Parent.Parent.Parent:IsA(“Folder”) and HitPart.Parent.Parent.Parent.Name == “EnemyFolder” then
for _,v in pairs(HitPart.Parent:GetChildren()) do
local enemy = v.Parent
local distance = (player.Position - v.Position).Magnitude
if distance < 3 then

				if v:IsA("BasePart") and v.Parent.Parent.Values.IsEnemy.Value == true then --if it has a value called IsEnemy that is true deal damage to it.
				player:WaitForChild("leaderstats"):WaitForChild("Coins").Value += 1
					Punch:Play()
					
				v.Parent.Parent.Values.Health -= 1
				local Damaged = enemy.Humanoid:LoadAnimation(Damaged)
					Damaged:Play()
					
				if v.Parent.Parent.Values.Health == 0 then
					player:WaitForChild("leaderstats"):WaitForChild("Coins").Value += 20
					local Death = enemy.Humanoid:LoadAnimation(Death)
						Death:Play()
					end
				end
            end
        end
    end 
end 

end)

but it still doesn’t work

how could i do that? please i’m very new to scripting i only got 2 month experience

I decided to do a quick Google search and I’ve found this script that might work.

local PlaceHolderValue = IntValue.Value -- sets placeholder value

IntValue.Changed:Connect(function()
    if PlaceHolderValue > IntValue.Value then -- checks to see if it decreased
        -- code here
        PlaceHolderValue = IntValue.Value -- sets PlaceHolderValue to the new value
    end
end)

Credit to CapnFry for the code, btw.

1 Like

THANKS SOO MUCH MY SCRIPT IS WORKING, thanks i didn’t think of this solution but its fixed Thanks for the solution

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.