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

UserInputService only when holding a Tool?

Asked by 5 years ago

So i have been trying this for a long time and still can't do it.How do i make it so that when the player is holding a tool THEN they can use UIS (UserInputService).Most of the time i can use the UIS without holding the tool and that is not what i am trying to get.

01wait(3)
02local UIS = game:GetService("UserInputService")
03local Player = game.Players.LocalPlayer
04local Holding = false
05 
06if script.Parent.Equipped == true then
07    Holding = true
08elseif script.Parent.Equipped == false then
09    Holding = false
10end
11 
12    UIS.InputBegan:Connect(function(key)
13        if key.KeyCode == Enum.KeyCode.R and Holding == true then
14            print("Key")
15             local BV = Instance.new("BodyVelocity", Player.Character.HumanoidRootPart)
View all 21 lines...
0
Not sure if this'll work but try script.Parent.Equipped:connect(function() Holding = true end) and the script.Parent.Unequipped:connect(function() Holding = false end) Asentis 17 — 5y
0
Doesn't work. I tried it before. DAsanicmaster 52 — 5y

2 answers

Log in to vote
0
Answered by
Asentis 17
5 years ago

I figured out the problem here.

There's a setting within the tool called 'RequiresHandle'. If this is enabled, the tool will wait for a handle and will yield the script.

If you don't want a handle, make sure that's disabled or else include a part within the tool called 'Handle'.

01local UIS       =       game:GetService("UserInputService")
02local Tool      =       script.Parent
03 
04local Equipped  =       false
05 
06UIS.InputBegan:connect(function(Input, GP)
07    if Input.UserInputType == Enum.UserInputType.Keyboard then
08        if GP ~= true then
09            if Equipped == true then
10                local Key = Input.KeyCode
11 
12                if Key == Enum.KeyCode.R then
13                    print("R was pressed!")
14                end
15 
View all 22 lines...
0
If this worked for you please mark it as the solution. If it didn't, however, please comment down what happened. Thanks. Asentis 17 — 5y
0
Thank you very much! You have saved me. (if you are wondering,I am making a Magic tool with multiple moves and since theis no handle i forgot to uncheck itre ) DAsanicmaster 52 — 5y
0
No problem. Asentis 17 — 5y
Ad
Log in to vote
0
Answered by
JakyeRU 637 Moderation Voter
5 years ago

Hello. Your variable Holding will be defined once and it will not change if you equip/unequip the tool. There are 2 events that will help you: Equipped and Unequipped. Here's an example:

01local UserInputService = game:GetService("UserInputService")
02local Holding = false
03 
04UserInputService.InputBegan:Connect(function(input, box)
05    if input.KeyCode == Enum.KeyCode.R and not box and Holding then
06        print("Key R pressed, tool equipped.!")
07    end
08end)
09 
10script.Parent.Equipped:Connect(function()
11    Holding = true
12end)
13 
14script.Parent.Unequipped:Connect(function()
15    Holding = false
16end)

Note: box is a boolean value. If the user was focused on a TextBox, for example, the Chat, when he was pressing the key, it will be true, if not, false.

0
Wish i could upvote but i need 25 reputation :( Asentis 17 — 5y
0
It apparently does not work. DAsanicmaster 52 — 5y
0
Tell me what errors do you have in the output? JakyeRU 637 — 5y
0
Also, are you running it in a Script or LocalScript? JakyeRU 637 — 5y
View all comments (3 more)
0
No errors,i tried running in Script and Local Script DAsanicmaster 52 — 5y
0
Run it in a LocalScript and make sure the LocalScript is direclty inside the tool. Not any descendants the tools has. JakyeRU 637 — 5y
0
Did exactly what you said and it still didn't work. DAsanicmaster 52 — 5y

Answer this question