So, I've been working on this script for a short while now, and with the help of another SH member, I've been able to get it working.
lscript = script.Parent:WaitForChild("LocalScript") animation = lscript:WaitForChild("ShunStart") animation1 = lscript:WaitForChild("Salute") animation2 = lscript:WaitForChild("RightDress") animation3 = lscript:WaitForChild("AtEaseLoop") local Player = game.Players.LocalPlayer local Mouse = Player:GetMouse() local animation = script.ShunStart local animation1 = script.Salute local animation2 = script.RightDress local animation3 = script.AtEaseLoop local animTrack = nil script.Parent.Selected:connect(function() Mouse.KeyDown:connect(function(key) if key == "q" then for _,v in pairs(Player.Character:FindFirstChild("Humanoid"):GetPlayingAnimationTracks()) do v:Stop(0) end animTrack = Player.Character:FindFirstChild("Humanoid"):LoadAnimation(animation) animTrack:Play() end end) Mouse.KeyDown:connect(function(key) if key == "r" then for _,v in pairs(Player.Character:FindFirstChild("Humanoid"):GetPlayingAnimationTracks()) do v:Stop(0) end animTrack = Player.Character:FindFirstChild("Humanoid"):LoadAnimation(animation2) animTrack:Play() end end) Mouse.KeyDown:connect(function(key) if key == "t" then for _,v in pairs(Player.Character:FindFirstChild("Humanoid"):GetPlayingAnimationTracks()) do v:Stop(0) end animTrack = Player.Character:FindFirstChild("Humanoid"):LoadAnimation(animation3) animTrack:Play() end end) Mouse.KeyDown:connect(function(key) if key == "e" then for _,v in pairs(Player.Character:FindFirstChild("Humanoid"):GetPlayingAnimationTracks()) do v:Stop(0) end animTrack = Player.Character:FindFirstChild("Humanoid"):LoadAnimation(animation1) animTrack:Play() wait(3) animTrack:Stop() animTrack = Player.Character:FindFirstChild("Humanoid"):LoadAnimation(animation) animTrack:Play() end end) Mouse.KeyDown:connect(function(key) local Key = key:lower() if Key == "p" then for _,v in pairs(Player.Character:FindFirstChild("Humanoid"):GetPlayingAnimationTracks()) do v:Stop(0) end end end) script.Parent.Deselected:connect(function() for _,v in pairs(Player.Character:FindFirstChild("Humanoid"):GetPlayingAnimationTracks()) do v:Stop(0) return end end) end)
The issue I seem to have is that, I tried to make it so that these animations would only play when the tool is selected, and that they'd stop being played once the tool is deselected.
So, for example; when the tool is selected, the animations will play when a key is pressed, but when the tool is not selected, the animations will not play when the keys are pressed.
It seems all that deselecting the tool does is stop the animations, which, whilst this is helpful, this really isn't what I wish to happen.
Again, help is much appreciated.
Before getting into anything else, you should really get in the habit of using UserInputService instead of mouse events for user input. My revision will be implementing UserInputService, so if you're unfamiliar with it, I suggest reading about it on the wiki before moving on.
When creating an event listener, you should make sure it's not in any circumstance where it will be re-created over and over again... Unless you plan on disconnecting them after you're done. In your script, you have it so that every time your character equips their tool, it creates all of those event listeners all over again. This is a very bad idea (in retrospect, you could have used one KeyDown event, and had several if/elseif statements for different keys, but I still do not recommend using the PlayerMouse object for user input).
Now I'll show you an example of how you can implement UserInputService in your code making it a lot shorter, organized, and efficient.
local InputService = game:GetService("UserInputService") local lscript = script.Parent:WaitForChild("LocalScript") local animation = lscript:WaitForChild("ShunStart") local animation1 = lscript:WaitForChild("Salute") local animation2 = lscript:WaitForChild("RightDress") local animation3 = lscript:WaitForChild("AtEaseLoop") local Player = game.Players.LocalPlayer local Mouse = Player:GetMouse() local Tool = script.Parent -- Creating a variable for this is nice too. local animation = script.ShunStart local animation1 = script.Salute local animation2 = script.RightDress local animation3 = script.AtEaseLoop local animTrack = nil local toolSelected = false -- Create a gate for when the Tool is selected -- When the tool is equipped, change 'toolSelected' to true allowing our gate in the InputBegan event to open. Tool.Equipped:connect(function() toolSelected = true end) -- When it's unequipped, revert the value. Tool.Unequipped:connect(function() toolSelected = false end) -- InputBegan listener InputService.InputBegan:connect(function(Input) local KeyInput = Input.KeyCode if toolSelected then -- Gate to make sure tool is selected if KeyInput == Enum.KeyCode.A then -- Code elseif KeyInput == Enum.KeyCode.B then -- Alternative outcome elseif KeyInput == Enum.KeyCode.C then -- Something else end end end)
If you have any questions, just let me know and I'll get to them asap.