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.
01 | wait( 3 ) |
02 | local UIS = game:GetService( "UserInputService" ) |
03 | local Player = game.Players.LocalPlayer |
04 | local Holding = false |
05 |
06 | if script.Parent.Equipped = = true then |
07 | Holding = true |
08 | elseif script.Parent.Equipped = = false then |
09 | Holding = false |
10 | end |
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) |
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'.
01 | local UIS = game:GetService( "UserInputService" ) |
02 | local Tool = script.Parent |
03 |
04 | local Equipped = false |
05 |
06 | UIS.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 |
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:
01 | local UserInputService = game:GetService( "UserInputService" ) |
02 | local Holding = false |
03 |
04 | UserInputService.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 |
08 | end ) |
09 |
10 | script.Parent.Equipped:Connect( function () |
11 | Holding = true |
12 | end ) |
13 |
14 | script.Parent.Unequipped:Connect( function () |
15 | Holding = false |
16 | end ) |
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.