Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
✓ ANSWER ACCEPTED

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

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)

1 comment

0

How about use Tool.Equipped and then on activated call it?

1 answer

✓ Accepted answer

###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)

This discussion is preserved as it was. New answers, comments, and votes are closed.