I need help on adding a feature on this script. It should activate an animation when the NPC hits an humanoid.
r = game:service("RunService") ching = false local mobs = script.Parent.Mobs:GetChildren() for i = 1, #mobs do function blow(hit) if ching == true then return end local humanoid = hit.Parent:FindFirstChild("Humanoid") if humanoid~=nil then local vCharacter = humanoid.Parent local vPlayer = game.Players:playerFromCharacter(vCharacter) dt = Game.Workspace.DynamicText:Clone() ching = true print("SWORD HIT") tagHumanoid(humanoid, vPlayer) local damage = math.random(mobs[i].SETTINGS.MinDmg.Value,mobs[i].SETTINGS.MaxDmg.Value) humanoid:TakeDamage(damage) local part = Instance.new("BillboardGui") part.Size = UDim2.new(0,50,0,100) part.StudsOffset = Vector3.new(0,2,0) local part2 = Instance.new("TextLabel") part2.FontSize = "Size24" part2.Font = "ArialBold" part2.Size = UDim2.new(1,0,1,0) part2.Position = UDim2.new(0,0,0,0) part2.BackgroundTransparency = 1 part2.Parent = part part2.TextStrokeTransparency = 0 dt.Parent = part2 dt.Disabled = false part.Parent = humanoid.Parent.Head part.Adornee = part.Parent if (damage == 0) then part2.TextColor3 = Color3.new(0/255,102/255,255/255) part2.Text = ""..damage.."" else part2.TextColor3 = Color3.new(255/255,0/255,0/255) part2.Text = "-"..damage.."" end wait(1) ching = false wait(.1) untagHumanoid(humanoid) else print("Humanoid does not exist or incorrect humanoid!") --Attempt to make sword unbreakable. end end function tagHumanoid(humanoid, player) local creator_tag = Instance.new("ObjectValue") creator_tag.Value = player creator_tag.Name = "creator" creator_tag.Parent = humanoid end function untagHumanoid(humanoid) if humanoid ~= nil then local tag = humanoid:FindFirstChild("creator") if tag ~= nil then tag.Parent = nil end end end mobs[i].Sword.Handle.Touched:connect(blow) end
You need to do a few things first; 1. Make the animation, I'm assuming you've already got one lined up, so that leads us onto number 2. Place the Animation either in a little folder in the NPC, or just the script, then path to the humanoid like so:
local Humanoid = pathtohumanoid local Animation = pathtoanimation local AnimTrack = Humanoid:LoadAnimation(Animation) --< this means the animations waiting AnimTrack:Play(fade) --< plays the animation. fade is default 0, but can transition with a time.
if you want it faster or slower,
AnimTrack:AdjustSpeed(Speed) --< Normal speed is 1, lower is slower, higher is faster AnimTrack:Stop(fade) --< again, default to 0, but can be transitioned.
Also, instead of creating a unique function for every enemy, use the same one and only create multiple connections:
function blow(hit, NPCmodel) --< ey, pretty clever! for i_,i in pairs(mobs) do mobs[i].Sword.Handle.Touched:connect(function(part) --<Super small! blow(part, mobs[i]) end) end