I'm trying to make a tool be able to have keybinds when equiped

i know i’m over looking something here. any suggestions? i want to make it to where a player has access to keybinds while a tool is being acctivated.

local tool = script.Parent
local uis = game:GetService("UserInputService")
local keypressed = true
local activated = true

tool.Activated:Connect(function()
	activated = true
	while activated == true do
		wait()
		uis.InputBegan:Connect(function(input,gameProcessedEvent)
			if input.KeyCode == Enum.KeyCode.E then
				keypressed = true
				print"yeet"
			end
		end)
	end
end)

tool.Deactivated:Connect(function()
	activated = false
	while activated == false do
		wait()
		uis.InputEnded:Connect(function(input,gameProcessedEvent)
			if input.KeCode == Enum.KeyCode.E then
				keypressed = false
				print"done"
			end
		end)
	end
end)

i already see how i messed up on user input. i need to put that all together in the first bit of code.

Okay, I think I can help you. What you could do is create the events like input began and input ended but set them as the value of a variable. This way, you can disconnect them when the tool is unequipped to stop being able to use them when not holding the tool.

Small snippet of Code for you to look at:

tool.Equipped:Connect(function()
      local InputConnection1 = uis.InputBegan:Connect(function(input, gameProcessedEvent )
             -- Whatever code you want here
      end)
      local InputConnection2 = uis.InputEnded:Connect(function(input, gameProcessedEvent )
             -- Whatever code you want here
      end)
      Tool.Unequipped:Connect(function()
             InputConnection1:Disconnect() -- Disconnect the first connection
             InputConnection2:Disconnect() - Disconnect the second connection
      end)
end)

Hope this helps!

2 Likes

thanks bro. i figured it out. i didn’t do it this exact way but something similar.