Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
✓ ANSWER ACCEPTED

How would I go about stopping the KeyDown function?

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.

2 comments

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.

0

Use regular tool objects, hopperbins are very outdated, and ROBLOX doesn't recommend use of them anymore.

1 answer

✓ Accepted answer

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.

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.

3 comments

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.

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?

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.

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