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 the animation play only when the tool is equipped?

I want this animation to play only when the tool is equipped. Currently it plays when ever you just press "y". How do I do this?

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 == 'y' then
        local animTrack = Humanoid:LoadAnimation(kicking)
        animTrack:Play() 

	end 
	end

	
	


 Mouse.KeyDown:connect(KeyDown)

3 answers

You need to make a line that tells the code if the player is holding out the tool then it can play the animation

1 comment

0

Can you show me an example?

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 ToolEquiped = false



local function KeyDown(key) 
    key = key:lower()
    if key == 'y'and ToolEquiped  then
        local animTrack = Humanoid:LoadAnimation(kicking)
        animTrack:Play() 

    end 
    end


tool.Equiped:connect(function()ToolEquiped = true end)
tool.Unequipped:connect(function()ToolEquiped = true end)

 Mouse.KeyDown:connect(KeyDown)

2 comments

0

Didn't work.

0

he spelt equipped wrong btw

Or you could disconnect a KeyDown function when equipped, than disconnect on unequipped. But i suppose either work. Here's my example:

local player = game:GetService("Players").LocalPlayer
local mouse = player:GetMouse()
local tool = script.Parent

local key

tool.Equipped:connect(function()
	key = mouse.KeyDown:connect(function()
		print("Holding tool")
	end)
end)

tool.Unequipped:connect(function()
	if key then
		key:disconnect()
		key = nil
	end
end)

But yeah, there are a few ways.

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