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

How to add an OnTouched Hotkey Punching animation?

Sorry once again, I sound like a beggar but, Could you teach me how I would do this. Also, that when you touch the humanoid it does like -5 damage. I would also like to know that each time when that person presses the certain key it changes the animation every time.

Much Thanks.

2 comments

0

I suggest you to check my answer :)

0

I know you don't want to sound like a begger, but most of these answers are on the wiki.... like random numners etc. Please read the wiki if you can.

3 answers

Put this in the localscript parented to the part in the weapon (hitbox)

local part = script.Parent -- here insert location of the part which will damage someone after hitting him
part.Touched:connect(function(hit)
if hit.Parent:FindFirstChild("Humanoid") then
local plr = game.Players:GetPlayerFronCharacter(hit.Parent)
if plr ~= nil then
hit.Parent.Humanoid.Health = hit.Parent.Humanoid.Health - 5 -- change damage here
end
end
end)

3 comments

0

I meant when you press the key you do a punch

0

ouch You need animatoons then. I cant do this because Im on phone.

0

I have animations but I do not know how to put it in the script :P

First of all, create animations.

Second, use this LINK to understand mechanics of animations.

And lastly, check Health property to see how you can damage the player.

Basically, there are a few examplesin these links that is very simple. For that, you need to insert "Animation" object into somewhere into the game, then you need to change their IDs to your animations' IDs. You said there are more than one animations so I will show you how to play animations randomly in each click. After you get your animations into a safe place(ServerStorage is pretty good for these stuff) you need to insert a LocalScript into StarterPack or StarterGui and, you can basically copy and paste this or read descriptions to fully understand and remake it. I am making it for three animations, you can change it later.

--Firstly, we create an array that will hold our animations. Let's say our animations in a folder named "Animations" and it's in ServerStorage. And let's say our animations' names are Anim1, Anim2 and Anim3
Anims = {
game.ServerStorage.Animations.Anim1, --See how we do this? Variant, Variant, Variant
game.ServerStorage.Animations.Anim2, --Never forget Commas unless you type last Variant
game.ServerStorage.Animations.Anim3  --We do not put Comma at the end of last Variant.
}--Didn't understand how arrays work? Check this [LINK](http://wiki.roblox.com/index.php?title=Table&redirect=no#Arrays).
--Also we will need a bool for stop punching without waiting.
Busy = false
Damage = 5--The damage punching will cause.

--Now we need to create our function, for keypress event(Keyboard Input). Check this [LINK](http://wiki.roblox.com/index.php?title=Keyboard_input) for more info about keyboard inputs.

--I will use UserInputService for my example
function onKeyPress(inputObject, gameProcessedEvent)
	if inputObject.KeyCode == Enum.KeyCode.F then
		--This far in this function, I just copied the function from the site above. Just changed KeyCode. Now it's our turn.
		if Busy == false then
			Busy = true
		
			--First, let's choose a random animation from our table/array
			RandomAnim = Anims[math.random(1,3)]
			--Now let's play it. Halfly copied from the roblox wiki site above :)
			--But first, we need to find our Character
			Player = script.Parent.Parent--This will basically find LocalPlayer. You can use game.Players.LocalPlayer instead too.
			Character = Player.Character--As long as player has a character, this won't cause an error.
			--Now we found character, we need our Humanoid to use LoadAnimation function.
			local animTrack = Character.Humanoid:LoadAnimation(RandomAnim)
			--Aaaand PLAY!
			animTrack:Play()
	
			--Animation playing is done so far. Now let's go on damaging.
			--BasePart's Touched event([LINK](http://wiki.roblox.com/index.php?title=API:Class/BasePart/Touched)) is all we need. But this time we will connect it inside. To stop double hitting we also will put a bool value.
			
			Hit = false
			Character["Right Arm"].Touched:connect(function(TP)
				if Hit == false then --If punch never touched to a person yet.
					--BTW TP is our parameter, which is touched part. If arm touches to other player's body, TP is other player's body, if arm touches to a car, it's car's part.
					if TP.Parent:FindFirstChild("Humanoid") then --Checking if TP's parent(Enemy character in this case) has a humanoid. If so, it's a person.
						TP.Parent.Humanoid.Health = TP.Parent.Humanoid.Health - Damage--Just setting Health property of Humanoid to a new number which is lower. 
						Hit = true --So person cannot hit until swinging another punch.
					end
				end
			end)
			Busy = false
		end
	end
end
 
game:GetService("UserInputService").InputBegan:connect(onKeyPress)--To connect to onKeyPress function. NEVER FORGET THIS.

I hope this helped you! Took my 20 minutes to type descriptions, study this hard :)

1 comment

0

This looks pretty good! I'll try this too!

i think i know how LEARN!

1 comment

0

I think I know how to not lose reputation. Type COMMENT if you are not DESCRIBING!

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