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?

local UserInputService = game:GetService("UserInputService")
local tool = script.Parent

local function onInputBegan(input,gameProcessed)

tool.Equipped:Connect(function()
    if input.KeyCode == Enum.KeyCode.F then

        print("it worked.")

    end

end)
end
UserInputService.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:

local UserInputService = game:GetService("UserInputService")
local tool = script.Parent

local equipped = false

tool.Equipped:Connect(function()
    equipped = true
end

tool.Unequipped:Connect(function()
    equipped = false
end

UserInputService.InputBegan:Connect(function(key)
    if key.KeyCode == Enum.KeyCode.F and equipped == true then
        --Do stuff
    end
end)
Ad

Answer this question