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

Trying to make a reload function on a gun?

Asked by 6 years ago

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.

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" Loterman23 10 — 6y
0
ContextActionService is a Service, so you need to use the :GetService() yeilding function for example game:GetService("ContextActionService") saSlol2436 716 — 6y

1 answer

Log in to vote
0
Answered by 6 years ago
Edited 6 years ago

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

0
Oh,and also, i don't recommend you to use ContextActionService, it's really hard :V. I recommend using UserInputService arthurgps2 67 — 6y
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. hiimgoodpack 2009 — 6y
0
Ok... but i still prefer UserInput :V arthurgps2 67 — 6y
0
Where did you find the disconnect method? I'm not seeing it in the api at all. Loterman23 10 — 6y
Ad

Answer this question