I'm trying to create gun logic for my game, the reloading works fine with the old depreciated keyboardInput method, but I don't really want to use something that's depreciated so I decided to try using the new method, and it's still firing after the tool has been put away, here's an example of what I have.
[StarterPack] [Tool] [LocalScript] [Handle]
Inside the tool is a simple localScript
local tool = script.Parent tool.Equipped:connect(function(mouse) local function reloadKey(actionName, userInputState, inputObject) if userInputState == Enum.UserInputState.Begin then print(tool.Name) end end game.ContextActionService:BindAction("keyPress", reloadKey, false, Enum.KeyCode.R) end)
If you equip the tool then un-equip it and press 'R' it will still call the reloadKey function, if you equip the tool and un-equip it 10 times, it will print the tool name 10 times. I'm trying to figure out a way to only have this run when the tool is equipped.
The trouble you are having is in the tool.Equipped
event. Every time you equip the tool, the function inside it will be "cloned". That's why it prints the tool name according to how many times it has been equipped.
Roblox though about this and added a feature that you can store events inside variables, so the script could "disconnect" the event, like this:
local tool = script.Parent tool.Equipped:connect(function(mouse) local function reloadKey(actionName, userInputState, inputObject) if userInputState == Enum.UserInputState.Begin then print(tool.Name) end end reloadCon=game.ContextActionService:BindAction("keyPress", reloadKey, false, Enum.KeyCode.R) --This will store the reload event so we can use it later end) tool.Unnequipped:connect(function() reloadCon:disconnect() --This will disconnect the event so it can't fired again end)
Hope this helped :D