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

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

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.

2 comments

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.

0

How would I do that?

1 answer

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!

3 comments

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

0

Put the function before the Equipped function, then fire the checkHit function inside of the KeyDown function

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.

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