Scripting Helpers is winding down operations and is now read-only. More info→
Ad
Log in to vote
0

How would I go about stopping the KeyDown function?

Asked by 8 years ago

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.

01lscript = script.Parent:WaitForChild("LocalScript")
02 
03animation = lscript:WaitForChild("ShunStart")
04animation1 = lscript:WaitForChild("Salute")
05animation2 = lscript:WaitForChild("RightDress")
06animation3 = lscript:WaitForChild("AtEaseLoop")
07 
08local Player = game.Players.LocalPlayer
09local Mouse = Player:GetMouse()
10local animation = script.ShunStart
11local animation1 = script.Salute
12local animation2 = script.RightDress
13local animation3 = script.AtEaseLoop
14local animTrack = nil
15 
View all 64 lines...

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.

0
To elaborate further, this is a localscript within a hopperbin. The issue I'm having is that when the tool is deselecting, the keys still trigger the animations. I wish to make it so that when the hopperbin is deselected, pressing the keys will not trigger the animations. BritishActuaI 44 — 8y
0
Use regular tool objects, hopperbins are very outdated, and ROBLOX doesn't recommend use of them anymore. ScriptGuider 5640 — 8y

1 answer

Log in to vote
0
Answered by 8 years ago
Edited 8 years ago

User input

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.

Creating event listeners

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).

Implementing UserInputService!

Now I'll show you an example of how you can implement UserInputService in your code making it a lot shorter, organized, and efficient.

01local InputService = game:GetService("UserInputService")
02 
03local lscript = script.Parent:WaitForChild("LocalScript")
04 
05local animation = lscript:WaitForChild("ShunStart")
06local animation1 = lscript:WaitForChild("Salute")
07local animation2 = lscript:WaitForChild("RightDress")
08local animation3 = lscript:WaitForChild("AtEaseLoop")
09 
10local Player = game.Players.LocalPlayer
11local Mouse = Player:GetMouse()
12local Tool = script.Parent -- Creating a variable for this is nice too.
13local animation = script.ShunStart
14local animation1 = script.Salute
15local animation2 = script.RightDress
View all 42 lines...

If you have any questions, just let me know and I'll get to them asap.

0
I really have no understanding of UserInputService, even after reading up on it on the wiki. I've tried to implement this into the script, however it doesn't seem to be working at all. BritishActuaI 44 — 8y
0
I believe it's the part of the script which ensures the tool is selected which is causing issues here, the script seems to work fine without it, but it's a part of the script that I need the most. Any help on this? BritishActuaI 44 — 8y
0
Nevermind, thank you very much. I ended up having to keep this inside a HopperBin, as it wouldn't work for me any other way, however it's much more efficient now. Your help is much appreciated. BritishActuaI 44 — 8y
Ad

Answer this question