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.
wait(3) local UIS = game:GetService("UserInputService") local Player = game.Players.LocalPlayer local Holding = false if script.Parent.Equipped == true then Holding = true elseif script.Parent.Equipped == false then Holding = false end UIS.InputBegan:Connect(function(key) if key.KeyCode == Enum.KeyCode.R and Holding == true then print("Key") local BV = Instance.new("BodyVelocity", Player.Character.HumanoidRootPart) BV.maxForce = Vector3.new(0, math.huge, 0) BV.Velocity = Vector3.new(0, 60, 0) wait(0.5) BV:Destroy() end end)
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'.
local UIS = game:GetService("UserInputService") local Tool = script.Parent local Equipped = false UIS.InputBegan:connect(function(Input, GP) if Input.UserInputType == Enum.UserInputType.Keyboard then if GP ~= true then if Equipped == true then local Key = Input.KeyCode if Key == Enum.KeyCode.R then print("R was pressed!") end end end end end) Tool.Equipped:connect(function() Equipped = true end) Tool.Unequipped:connect(function() Equipped = false end)
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:
local UserInputService = game:GetService("UserInputService") local Holding = false UserInputService.InputBegan:Connect(function(input, box) if input.KeyCode == Enum.KeyCode.R and not box and Holding then print("Key R pressed, tool equipped.!") end end) script.Parent.Equipped:Connect(function() Holding = true end) script.Parent.Unequipped:Connect(function() Holding = false 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.