Scripting Helpers is winding down operations and is now read-only. More info→
Ad
Log in to vote
0

how to make when pressing "E" punch animation?

Asked by 3 years ago

How to do when you press "E" punch animation plays and causes damage to the player I punched?

0
How good are you at scripting? radiant_Light203 1166 — 3y

2 answers

Log in to vote
0
Answered by
sngnn 274 Moderation Voter
3 years ago

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)
0
THANKS ITS WORKIG! VIP_666X 2 — 3y
0
2 months later... apparently, you should instead use Humanoid.Animator. sngnn 274 — 3y
Ad
Log in to vote
0
Answered by 3 years ago

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!

Answer this question