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

How to make a tool script only do stuff if the tool is equipped?

Asked by 5 years ago
Edited 5 years ago

Upon clicking the tool becomes activated, allowing you to press keys to do stuff. The problem is that when you unequip the tool it's still activated I guess, so the stuff still happens when you press the corresponding key.

function onActivated()

uis.InputBegan:connect(function(input,process)
if not process and  input.KeyCode == Enum.KeyCode.V
--Stuff happens here
end end)

end

script.Parent.Activated:connect(onActivated)
0
How about use Tool.Equipped and then on activated call it? halfvedantpandya1234 6 — 5y

1 answer

Log in to vote
1
Answered by 5 years ago

To fox that you'll need a boolean, enable on activation, disable on unequip.

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

UserInputService.InputBegan:Connect(function(inp, gpe) -- switch to :Connect, :connect is deprecated 

    if gpe and not active then return end  

    if inp.KeyCode == Enum.KeyCode.V and activated then 
        -- code
    end
end)

tool.Activated:Connect(function()
    active = true -- activate on activation 
end)

tool.Unequipped:Connect(function(mouse)
    active = false -- deactivate 
end)
Ad

Answer this question