How to do when you press "E" punch animation plays and causes damage to the player I punched?
You'd first need to detect the key input, which can be done by:
local UIS = game:GetService('UserInputService') UIS.InputBegan:Connect(function(key) --detect key input if key.KeyCode == Enum.KeyCode.E then --checks if the input was the E key -- do stuff (explained later) -- end end)
If the user inputted "E", we can go ahead and play an animation. Humanoid:LoadAnimation()
has been deprecated, so we should instead use an Animator.
local UIS = game:GetService('UserInputService') local Animator = Instance.new('Animator') local Animation = Instance.new('Animation') local Player = game:GetService('Players').LocalPlayer local Character = Player.Character or Player.CharacterAdded:Wait() --get the player/character Animator.Parent = Character:WaitForChild('Humanoid') Animation.Parent = Animator Animation.AnimationId = 'rbxassetid://animationid' UIS.InputBegan:Connect(function(key) --detect key input if key.KeyCode == Enum.KeyCode.E then --checks if the input was the E key local Track = Animator:LoadAnimation(Animation) Track:Play() -- if so, load and play an animation end end)
As for the damaging part, we can just detect when a body part is hit while the animation is still playing. Something like:
Character['Left Arm'].Touched:Connect(function(hit) if hit.Parent:FindFirstChild('Humanoid') and Track.IsPlaying then hit.Parent.Humanoid:TakeDamage(5) end end)
You would need a local script to play the animation and a server script to inflict the damage to the humanoid (player/npc)
for example, local script has anim play and server script has a script that enables damage to the punch while anim play time is on.
hope it could help!