I’m making on Game named: Punching simulator, And In the past Day I’ve been struggling to make the Punch enemy script:
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.
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.
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)
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
–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
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)