How could I detect a keypress from inside a tool?

Hello. I need help detecting a keypress from inside a tool.
I have a tool with a localscript in it, and inside the localscript there is a remoteevent.
This is the localscript code:

local tool = script.Parent
local equipped = false
local UID = game:GetService("UserInputService")

tool.Equipped:Connect(function()
	equipped = true
end)

tool.Unequipped:Connect(function()
	equipped = false
end)


UID.InputBegan:Connect(function(key,isTyping)
	if isTyping == false then
		if equipped == true then
			print("tool equipped")
			if key == Enum.KeyCode.F then
				print("pressed f")
		script.Fire:FireServer()
				print("fired server")
				end
			end
	end
end)

Everytime I test the tool, it only prints the “tool equipped” - not anything else.

I don’t know what’s wrong with the code. I want for the remote event to fire everytime the player presses F while the tool is equipped.

Thanks!

doing “if key” would work better with KeyDown; Mouse | Documentation - Roblox Creator Hub

1 Like

Judging from the API description (InputObject | Documentation - Roblox Creator Hub) you actually need to use key.KeyCode.

local tool = script.Parent
local equipped = false
local UID = game:GetService("UserInputService")

tool.Equipped:Connect(function()
	equipped = true
end)

tool.Unequipped:Connect(function()
	equipped = false
end)


UID.InputBegan:Connect(function(key,isTyping)
	if isTyping == false then
		if equipped == true then
			print("tool equipped")
			if key.KeyCode == Enum.KeyCode.F then
				print("pressed f")
                script.Fire:FireServer()
				print("fired server")
				end
			end
	end
end)

Can you try this?

5 Likes

Isn’t that deprecated? [30 characters]

2 Likes

Oh sorry, yes it is but it still works because its just outdated.

2 Likes