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

How to track keypressed in a tool?

Hi, I'm using userinputservice. I wanted to know how to make it only track input when a tool is equipped, I tried the script below but it just didnt register at all when equipped. I'm out of ideas for it lol

local uis = game:GetService("UserInputService")
local attacking = false

script.Parent.Equipped:connect(function()
	input1 = uis.InputBegan:connect(function(input, gp)
	if gp then
		return
		
	else
		if input.KeyCode == Enum.KeyCode.Q then
			print("pressed")
		end
	end
end)

input2 = uis.InputEnded:connect(function(input, gp)
	if gp then
		return
		
	else
		if input.KeyCode == Enum.KeyCode.Q then
			print("released")
		end
	end
end)
end)

script.Parent.Unequipped:connect(function()
	input1:Disconnect()
	input2:Disconnect()
end)

2 answers

✓ Accepted answer

You can create a variable that holds a boolean value that changes on wether or not you equipped the tool

Then create a function that trigger when InputBegan, and use if function using the boolean value you created

Example

local Equipped = false
local input = game:GetService("UserInputService")
script.Parent.Equipped:connect(function()
Equipped = true
end)

script.Parent.Unequipped:connect(function()
Equipped = false
end)

input.InputBegan:connect(function(inp, proc)
if Equipped then
if inp.KeyCode == Enum.KeyCode.Q then
Print("Q is pressed!")
end
if inp.KeyCode == Enum.KeyCode.E then
Print("E is pressed")
end
end
end)

input.InputEnded:connect(function(inp,proc)
If Equipped then
-- i believe you can just write this part yourself
end
end)

Excuse any bug since i write them on a smartphone

5 comments

0

I responded back in a seperate answer

0

You should do this instead, because the Equipped event is supposed to fire the "moment" you equipped the tool, and it doesn't keep on firing as long as your holding it it doesn't keep on firing

0

indent!!!

0

Indent your code

0

Oh ffs, another simon cowelss of answer, didnt you reas? I wrote them using a phone geniuses

For some reason that didn't work, I might be clowning but this is what I wrote with that info:

local uis = game:GetService("UserInputService")
local attacking = false
local equipped = false

script.Parent.Equipped:connect(function()
	equipped = true
end)	

script.Parent.Unequipped:connect(function()
	equipped = false
end)

uis.InputBegan:connect(function(input, gp)
	if equipped then
		if gp then
		return
		
	else
		if input.KeyCode == Enum.KeyCode.Q then
			print("pressed")
		end
	end
	else
		return
	end
end)

uis.InputEnded:connect(function(input, gp)
	if equipped then
		if gp then
		return
		
	else
		if input.KeyCode == Enum.KeyCode.Q then
			print("released")
		end
	end
	else
		return
	end
	
end)

Just like before there are no errors in output, it's just nothing is printed when Q is pressed and released regardless of equip status.

4 comments

0

Do your tool have a handle? Or "RequiresHandle" value in the property of the tool is set to false?

0

omg im sooo dumb i forgot I used the disconnect method on all my other tools I just thought it hated me lol

0

Please correctly indent your code

0

it is, scripting helpers ruins formatting.

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