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

Detect if a player press a key when the tool is equipped?

Asked by 6 years ago

Hello I would like to make a skill for a game only work when the tool is equipped so I wrote the following script for example and it's not working. Any ideas on how to fix it ?

local uis = game:GetService('UserInputService')
local plr = game.Players.LocalPlayer
script.Disabled = false

script.Parent.Equipped:connect(function()
script.Disabled = false
uis.InputBegan:Connect(function(input)
    if input.KeyCode == Enum.KeyCode.E then
        print('The tool was equipped')
    end
end)
end)

script.Parent.Unequipped:connect(function()
script.Disabled = true
uis.InputBegan:Connect(function(input)
    if input.KeyCode == Enum.KeyCode.E then
        print('The tool was unequipped')
    end
end)
end)
1
Why at the beginning do you set the script's Disabled to false? Is it disabled in the first place? Bluemonkey132 194 — 6y

1 answer

Log in to vote
0
Answered by
Goulstem 8144 Badge of Merit Moderation Voter Administrator Community Moderator
6 years ago
Edited 6 years ago

Okay firstly.. you can't mess with the script's Disabled property.

I suggest making an a variable true when you equip, and simply checking it upon key press.

local uis = game:GetService('UserInputService')
local plr = game.Players.LocalPlayer
local eq = false --Equipped variable

script.Parent.Equipped:Connect(function() --'connect' is deprecated
    eq = true; --set equipped variable to true
    script.Parent.Unequipped:Wait(); --wait for them to unequip
    eq = false; --set equipped variable to false
end)

uis.InputBegan:Connect(function(input,process)
    if not process then --Make sure they're not chatting
        if input.KeyCode == Enum.KeyCode.E then
            --check equipped val
            print(eq and "The tool is equipped" or "The tool is unequipped");
        end
    end
end)
0
Thanks ScriptAbyss 10 — 6y
Ad

Answer this question