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)
You need to make a line that tells the code if the player is holding out the tool then it can play the animation
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)
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.