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

i am trying to make a key function when the tool is equipped. the scripts seems to not run?

Asked by 5 years ago

so i have a tool in the starterpack, and within this tool there is a localscript.

so im trying to make it so that you can ONLY activate this key function when the tool is equipped, but the script seems to not run. if there is a bug, what could it be?

01local UserInputService = game:GetService("UserInputService")
02local tool = script.Parent
03 
04local function onInputBegan(input,gameProcessed)
05 
06tool.Equipped:Connect(function()
07    if input.KeyCode == Enum.KeyCode.F then
08 
09        print("it worked.")
10 
11    end
12 
13end)
14end
15UserInputService.InputBegan:connect(onInputBegan)

1 answer

Log in to vote
0
Answered by 5 years ago

Just have a variable that tells whether the tool is equipped that changes whenever it's equipped and unequipped. Here's an example:

01local UserInputService = game:GetService("UserInputService")
02local tool = script.Parent
03 
04local equipped = false
05 
06tool.Equipped:Connect(function()
07    equipped = true
08end
09 
10tool.Unequipped:Connect(function()
11    equipped = false
12end
13 
14UserInputService.InputBegan:Connect(function(key)
15    if key.KeyCode == Enum.KeyCode.F and equipped == true then
16        --Do stuff
17    end
18end)
Ad

Answer this question