In my tool I've got a local script which runs an animation when holding the tool and the key is pressed. In the tool there is the Animation folder with the animations and there is the handle and local script. The Problem I have is when I press the key the animation plays but when I click the key again it doesn't stop playing.
Here is my script
local player = script.Parent.Parent.Parent --Gets the player if this is a regular tool in starterpack local character = player.Character or player.CharacterAdded:Wait() local tool = script.Parent local mouse = player:GetMouse() local UserInputService = game:GetService("UserInputService") -- Bools -- local toolon = false local isblocking = false -- Animations -- local Block = script.Parent.Animations.Block local animationTrack tool.Equipped:Connect(function() toolon = true end) tool.Unequipped:Connect(function() toolon = false end) UserInputService.InputBegan:connect(function(i,g) if i.UserInputType == Enum.UserInputType.Keyboard then if i.KeyCode == Enum.KeyCode.E and toolon == true and isblocking == false then animationTrack = character:WaitForChild("Humanoid"):LoadAnimation(Block) animationTrack:Play() print("Block Is On") isblocking = true end end end) UserInputService.InputBegan:connect(function(i,g) if i.UserInputType == Enum.UserInputType.Keyboard then if i.KeyCode == Enum.KeyCode.E and toolon == true and isblocking == true then animationTrack:Stop() print("Block Is Off") isblocking = false end end end)
UPDATE: I've edited the code and changed it but it still doesn't work, Here is the new gif https://gyazo.com/985aef884e981d7318ce9fbd149f5757 Here is the new Rbxl https://mega.nz/file/nNxRTZRB#N_XZwyc8_QxjP0BQZdjCxdjxI6a_sPOCHEz_7JViynU
You are loading the animation only inside the input function. It will work if you call LoadAnimation outside the functions so both of them can work with that same animation, like this:
local variables = etc -- Animations -- local Block = script.Parent.Animations.Block local animationTrack = character:WaitForChild("Humanoid"):LoadAnimation(Block) UserInputService.InputBegan:connect(function(i,g) if i.UserInputType == Enum.UserInputType.Keyboard then if i.KeyCode == Enum.KeyCode.E and toolon == true and isblocking == false then animationTrack:Play() print("Block Is On") isblocking = true end end end) UserInputService.InputBegan:connect(function(i,g) if i.UserInputType == Enum.UserInputType.Keyboard then if i.KeyCode == Enum.KeyCode.E and toolon == true and isblocking == true then animationTrack:Stop() print("Block Is Off") isblocking = false end end end)
JUST TO SIMPLIFY YOUR CODE WRITING:
UserInputService.InputBegan:connect(function(i,g) if i.UserInputType == Enum.UserInputType.Keyboard -- and and i.KeyCode == Enum.KeyCode.E and toolon == true and isblocking == false then animationTrack:Play() print("Block Is On") delay(0.1, function() isblocking = true end) -- prevents from making this script crazy. elseif i.UserInputType == Enum.UserInputType.Keyboard -- only one connection to input and i.KeyCode == Enum.KeyCode.E and toolon == true and isblocking == true then animationTrack:Stop() print("Block Is Off") delay(0.1, function() isblocking = false end) end end)
It's generally better practice to use UserInputService but what I'd suggest is separate the if statements instead of using elseif. Usually when I have a problem with elseif, I solve it by making them two separate if statements.
Edit: You never stopped the animation when the tool was unequipped so I'm pretty sure that's a result.