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

Trying to make a reload function on a gun?

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.

2 comments

0

Incase anyone else runs into this and needs help with it, every time ContextActionService:BindAction() is called, you have to unbind it when you don't want it to listen anymore using ContextActionService:UnbindAction(). Where I went wrong in my post was, every time I equipped the tool it would create a new instance of the ContextActionService keyPress"

0

ContextActionService is a Service, so you need to use the :GetService() yeilding function for example game:GetService("ContextActionService")

1 answer

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

5 comments

0

Oh,and also, i don't recommend you to use ContextActionService, it's really hard :V. I recommend using UserInputService

0

Just because something is hard, does not mean you should not use it. However, if it had security issues, then that would be a reason to not use it.

0

Ok... but i still prefer UserInput :V

0

Where did you find the disconnect method? I'm not seeing it in the api at all.

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