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

Punch animation: Damage not working?

Asked by 8 years ago

There is nothing wrong with the animation. Only the damage part is wrong


player = game.Players.LocalPlayer animation = script:WaitForChild("Animation") enabled = true ra = player.Character:WaitForChild("Right Arm") dmg = script.Damage.Value game:GetService("UserInputService").InputBegan:connect(function(input,proc) if input.KeyCode == Enum.KeyCode.Q then if enabled then enabled = false function onTouch(ra) local humanoid = ra.Parent:FindFirstChild("Humanoid") --Im not sure if the humanoid is being properly indentified if humanoid ~= nil then humanoid.Health.Value = humanoid.Health.Value - dmg -- Where the damage won't work end ra.Touched:connect(onTouch) end local animationTrack = player.Character.Humanoid:LoadAnimation(animation) animationTrack:Play() wait(0.5) enabled = true end end end)

1 answer

Log in to vote
2
Answered by
M39a9am3R 3210 Moderation Voter Community Moderator
8 years ago

Problem

Well, you've got a couple of them, not as bad as I've seen. You're trying to connect to a function with the connection line inside the function. With this, the function will never get called.

Also, having the onTouched function inside the InputBegan function will just keep recreating that event meaning your punches will quickly get stronger.


Solution

What I've done was move the connection line out of the function. And moved the function out of the InputBegan connection function.


Final Script

player = game.Players.LocalPlayer

animation = script:WaitForChild("Animation")

enabled = true

ra = player.Character:WaitForChild("Right Arm")

dmg = script.Damage.Value

game:GetService("UserInputService").InputBegan:connect(function(input,proc)
    if  input.KeyCode == Enum.KeyCode.Q then
        if enabled then
            enabled = false

            local animationTrack = player.Character.Humanoid:LoadAnimation(animation)
            animationTrack:Play()

            wait(0.5)
            enabled = true
        end
    end
end)

function onTouch(ra)
    local humanoid = ra.Parent:FindFirstChild("Humanoid") --Im not sure if the humanoid is being properly indentified
    if humanoid ~= nil and enabled == true then 
        humanoid.Health.Value = humanoid.Health.Value - dmg -- Where the damage won't work
    end
end
ra.Touched:connect(onTouch)

Hopefully this answer helped. If it did hit the upvote button and if it answered your question, hit the Accept Answer button. Also, if there are any questions or you're getting an error, feel free to comment below!
0
How would you make the script punch from both hands? FireyMcBlox 134 — 6y
Ad

Answer this question