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.

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

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

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.

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