Ad
Log in to vote
1

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

Asked by 5 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 — 5y

1 answer

Log in to vote
0
Answered by
Goulstem 8144 Badge of Merit Moderation Voter Administrator Community Moderator
5 years ago
Edited 5 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 — 5y
Ad

Answer this question