Scripting Helpers is winding down operations and is now read-only. More info→
Ad
Log in to vote
0

How to track keypressed in a tool?

Asked by
Gojinhan 353 Moderation Voter
4 years ago

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

Log in to vote
0
Answered by 4 years ago

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

0
I responded back in a seperate answer Gojinhan 353 — 4y
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 starmaq 1290 — 4y
0
indent!!! ThatPreston 354 — 4y
0
Indent your code Leamir 3138 — 4y
0
Oh ffs, another simon cowelss of answer, didnt you reas? I wrote them using a phone geniuses Azure_Kite 885 — 4y
Ad
Log in to vote
0
Answered by
Gojinhan 353 Moderation Voter
4 years ago

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.

0
Do your tool have a handle? Or "RequiresHandle" value in the property of the tool is set to false? Azure_Kite 885 — 4y
0
omg im sooo dumb i forgot I used the disconnect method on all my other tools I just thought it hated me lol Gojinhan 353 — 4y
0
Please correctly indent your code Leamir 3138 — 4y
0
it is, scripting helpers ruins formatting. Gojinhan 353 — 4y

Answer this question