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

Help on punching animation so it can do damage?

I made a punching animation but I am trying to make my animation do damage to dummies, but the animation wont play, and wont do damage! If anyone can tell me what I'm doing wrong that would help a lot.

My code:

player=game.Players.LocalPlayer
mouse=player:GetMouse()

function KeyD (key)
	key=key:lower()
	local hotkey=script.Hotkey
	local char=player.Character
	if key==hotkey.Value then
		local dance=Instance.new("Animation")
		dance.AnimationId="https://web.roblox.com/Punch-item?id=476354090"
		local animloader=char.Humanoid:LoadAnimation(dance)
		animloader: Play()
	end
end
script.Parent.Touched:connect(function(part)
	local human=part.Parent:findFirstChild("Humanoid")
	if human then
		if human.Parent.Name~=script.Parent.Parent.Parent.Name and
CanDamage == true and script.Parent.Parent.Swinging.Value==true then
            CanDamage=false
			human:TakeDamage(20)
			wait(1.7)
			CanDamage=true
		end
	end
end)
mouse.KeyDown:connect(KeyD)

1 answer

I can see numerous issues, ill just post a fixed version of the code and write what is changed.

local Player = game.Players.LocalPlayer
local Character = Player.Character
local Humanoid = Character:WaitForChild("Humanoid")
local Animation = Instance.new("Animation")
Animation.AnimationId = "rbxassetid://476354090" -- For asset lookup you must use "rbxassetid://assetid" or "http://www.roblox.com/asset/?id=assetid" not the website url
local LoadedAnimation = Humanoid:LoadAnimation(Animation) -- Load the animation only once

local UserInputService = game:GetService("UserInputService") -- Use UserInputService as Mouse.KeyDown is depreciated

local CanDamage = false
LoadedAnimation.Stopped:connect(function()
	CanDamage = false -- Self explainitary, stops damage when animation stops
end)

UserInputService.InputBegan:connect(function(Input, GME)
	if not GME then -- GME means GameProcessedEvent, checking if its false stops the command working if a menu is open, more info here http://wiki.roblox.com/?title=API:Class/UserInputService
		if Input.KeyCode == Enum.KeyCode[script.Hotkey.Value] then -- Checking whether the key is the required one by turning the string into a keycode enum
			LoadedAnimation:Play()
			CanDamage = true
		end
	end
end)

local Cooldown = false
script.Parent.Touched:connect(function(Part)
	local Human = Part.Parent:findFirstChild("Humanoid")
	if Human and Human.Parent.Name ~= Character.Name and CanDamage and not Cooldown then
		Cooldown = true
		Human:TakeDamage(20)
		wait(1.7)
		Cooldown = false
	end
end)

5 comments

0

thanks man, sorry just started playing roblox like a week ago

0

do i put this script in the starter pack or?

0

Should function exactly the same as the one you posted, of which i assume was a tool?

0

Do I just put it in the tool?

0

Inside the handle or the part that you want to be the hit box for damage. Or you can put it directly in the tool and change line 25 "script.Parent.Touched:connect(function(Part)" to "script.Parent.Handle.Touched:connect(function(Part)"

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