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

How to add an OnTouched Hotkey Punching animation?

Asked by 7 years ago
Edited 7 years ago

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.

0
I suggest you to check my answer :) superalp1111 662 — 7y
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. H4X0MSYT 536 — 7y

3 answers

Log in to vote
0
Answered by
Etheroit 178
7 years ago

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)
0
I meant when you press the key you do a punch Daiko_King 1 — 7y
0
ouch You need animatoons then. I cant do this because Im on phone. Etheroit 178 — 7y
0
I have animations but I do not know how to put it in the script :P Daiko_King 1 — 7y
Ad
Log in to vote
0
Answered by 7 years ago

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 :)

0
This looks pretty good! I'll try this too! marioblast1244 113 — 7y
Log in to vote
-1
Answered by 7 years ago

i think i know how LEARN!

0
I think I know how to not lose reputation. Type COMMENT if you are not DESCRIBING! superalp1111 662 — 7y

Answer this question