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 7 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 ?

01local uis = game:GetService('UserInputService')
02local plr = game.Players.LocalPlayer
03script.Disabled = false
04 
05script.Parent.Equipped:connect(function()
06script.Disabled = false
07uis.InputBegan:Connect(function(input)
08    if input.KeyCode == Enum.KeyCode.E then
09        print('The tool was equipped')
10    end
11end)
12end)
13 
14script.Parent.Unequipped:connect(function()
15script.Disabled = true
View all 21 lines...
1
Why at the beginning do you set the script's Disabled to false? Is it disabled in the first place? Bluemonkey132 194 — 7y

1 answer

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

01local uis = game:GetService('UserInputService')
02local plr = game.Players.LocalPlayer
03local eq = false --Equipped variable
04 
05script.Parent.Equipped:Connect(function() --'connect' is deprecated
06    eq = true; --set equipped variable to true
07    script.Parent.Unequipped:Wait(); --wait for them to unequip
08    eq = false; --set equipped variable to false
09end)
10 
11uis.InputBegan:Connect(function(input,process)
12    if not process then --Make sure they're not chatting
13        if input.KeyCode == Enum.KeyCode.E then
14            --check equipped val
15            print(eq and "The tool is equipped" or "The tool is unequipped");
16        end
17    end
18end)
0
Thanks ScriptAbyss 10 — 7y
Ad

Answer this question