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