Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
✓ ANSWER ACCEPTED

Punch animation: Damage not working?

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

✓ Accepted answer

##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!

1 comment

0

How would you make the script punch from both hands?

This discussion is preserved as it was. New answers, comments, and votes are closed.