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

Punch animation damage not working?

Asked by 6 years ago

I KNOW KEYDOWN IS DEPRECATED

Hi, I made an animation where I give a punch. Now, I'd like to give damage when a player or an humanoid get's hit by it. I tried this, not working though.

local player = game.Players.LocalPlayer
repeat wait() until player.Character.Humanoid
local humanoid = player.Character.Humanoid
local mouse = player:GetMouse()
local db = false
local anim = Instance.new("Animation")
local damage = game.ReplicatedStorage.Damage.Value
anim.AnimationId = "http://www.roblox.com/asset/?id=2055511545" -- id of animation


mouse.KeyDown:connect(function(key)
  if db == false then
    if key == "q" then
        db = true
        local playAnim = humanoid:LoadAnimation(anim)
        playAnim:Play()
        wait(1)
        db = false
        game.Players.LocalPlayer.Character.RightHand.Touched:Connect(function(plr)
            local huma = plr:WaitForChild("Humanoid")
            if huma then
                huma.Health = huma.Health - damage

            end
        end)
    end
end
end)
0
If you know KeyDown is deprecated, why are you using it? User#19524 175 — 6y

3 answers

Log in to vote
1
Answered by
luachef 61
6 years ago

The problem is that the passed variable "plr" is the part that touched your right hand. That means that you have to search for the humanoid in the plr's parent, not in plr.

local player = game.Players.LocalPlayer
repeat wait() until player.Character.Humanoid
local humanoid = player.Character.Humanoid
local mouse = player:GetMouse()
local db = false
local anim = Instance.new("Animation")
local damage = game.ReplicatedStorage.Damage.Value
anim.AnimationId = "http://www.roblox.com/asset/?id=2055511545" -- id of animation


mouse.KeyDown:connect(function(key)
  if db == false then
    if key == "q" then
        db = true
        local playAnim = humanoid:LoadAnimation(anim)
        playAnim:Play()
        wait(1)
        db = false
        game.Players.LocalPlayer.Character.RightHand.Touched:Connect(function(part)
            local huma = part.Parent:FindFirstChild("Humanoid")
            if huma then
                huma.Health = huma.Health - damage
            end
        end)
    end
end
end)

also i would recommend to use :TakeDamage() function on the humanoid so people can't kill players with forcefields etc., but it's up to you.

0
btw i renamed the plr variable to "part" so you understand the thing i've done. luachef 61 — 6y
Ad
Log in to vote
1
Answered by 6 years ago

You shouldn’t use KeyDown, use UserInputService or ContextActionService.

-- LocalScript

local plr = game:GetService("Players").LocalPlayer
local char = plr.Character or plr.CharacterAdded:Wait()
local dmg = 10 -- set to your damage

function punch()
    if not char:FindFirstChild'PunchAnim' then
        local anim = Instance.new'Animation'
        anim.Name = 'PunchAnim'
        anim.AnimationId = '' -- put your ID here
        anim.Parent = char
    end

    local dmgScript = script.DmgScript:Clone()
    dmgScript.Parent = char.RightHand
    dmgScript.Disabled = false
    wait(0.5)
    dmgScript:Destroy()
end

game:GetService("ContextActionService"):BindAction(
    "Punch",
    punch,
    false,
    Enum.KeyCode.Q
)

Separate server script under the punching local script:

-- Server Script

local dmg = 10

script.Parent.Touched:Connect(function(part)
    if part.Parent:FindFirstChild"Humanoid" then
        if part.Parent ~= script.Parent.Parent then
            part.Parent.Humanoid:TakeDamage(dmg)
        end
    end
end)
Log in to vote
0
Answered by 5 years ago

just take a sword script, make the mesh invisible and replace the animations, and change the range and/or damage if you need to

Answer this question