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

How do I make this animation work only when the tool is Equipped?

Asked by 8 years ago

So currently if you press 't' a kicking animation occurs, but I wan't it to only happen if you have the tool equipped. How would I do that?

local Player = game.Players.LocalPlayer
local Character = Player.Character or Player.CharacterAdded:wait()
local Humanoid = Character:WaitForChild("Humanoid")
local tool = script.Parent

local Mouse = Player:GetMouse()

local kicking = Instance.new("Animation", Humanoid)

kicking.AnimationId = "http://www.roblox.com/item.aspx?id=273619670"

local function keyDown(key)
    key = key:lower()
    if key == 't' then
        local animTrack = Humanoid:LoadAnimation(kicking)
        animTrack:Play() 
    end
 end


function checkHit()
    local rLeg = Character:WaitForChild("Right Leg")
    local damage = 15

    rLeg.Touched:connect(function(toucher)
      if toucher.Parent.Humanoid then
          local hitHuman = toucher.Parent.Humanoid
         hitHuman.Health = hitHuman.Health - damage
     elseif toucher.Humanoid then
         local hitHuman2 = toucher.Humanoid
         hitHuman2.Health = hitHuman2.Health - damage
     end 
end)

end

Mouse.KeyDown:connect(keyDown, checkHit)



Yes It's inside of a tool, and It's a local script.

0
I'd create a local named equipped, keep it as false. when equipped fires, make the value true. when unequip fires, make it false. It's what I did for my check script. HungryJaffer 1246 — 8y
0
How would I do that? 64batsalex 45 — 8y

1 answer

Log in to vote
0
Answered by
Mr_Octree 101
8 years ago

Instead of being hassled with connecting the functions at the end of the script, you can connect them on the same line.

tool.Equipped:connect(function()
    Mouse.KeyDown:connect(function(key)
        key = key:lower()
        if key == "t" then
            local animTrack = Humanoid:LoadAnimation(kicking)
            animTrack:Play()
        end
    end)
end)

Now you don't have to connect every event to a function at the end of the script :D

Hope this helps!

0
At the end of the script I have something that deals damage to a humanoid when hit with the right leg, but it keeps crashing because there's no humanoid in workspace, and it's doing so without the animation 64batsalex 45 — 8y
0
Put the function before the Equipped function, then fire the checkHit function inside of the KeyDown function Mr_Octree 101 — 8y
0
So put the code in line 2 on line 1 and the one on line 1 on line2? I put the checkHit like this Mouse.KeyDown:connect(function(key, checkHit) and it didn't work. The animation didn't play. 64batsalex 45 — 8y
Ad

Answer this question