Creating a fire select keybind for my gun system

Hey guys, Queue here. Just need a little assistance. So, I’m in the midst of creating a new game that involves guns. I wanted to add fire select, in which I’m in the process of doing. Currently, all of the change-able variables are located in a module script. Now, with the fire select function, in order to all for some customization and save on some lines of code, I’ve stored all the possible firing modes in a separate table in my module, see image below.


I have the table set up as well as the key-bind function set up as well/ (see image below as well)


Now, what has me stumped is how would I go about programming a function in which when a key is pressed, returns a value (value in this case being a firing mode) in the array, and when pressed again, returns the other values within my array?

Other Comments

  • The reason I choose to store the firing modes in that array instead of writing in the main script within the function is
    A. I want to keep the system customize-able as possible
    B. The main script is linked with a package link
    C. I want to keep the time manually configuring options in my system as low as possible
    D. Some guns may not be able to select through a certain mode for realism reasons and such, so it’d be better if each gun had their own personal firing selection modes.
  • Are there other ways that I can approach this without needing to create an array with the settings module?

Thank you for the assistance/feedback! If you need any other images to better find the solution(s), mention me in a reply and I’ll get back to you ASAP.

1 Like

I think by array you mean table.

I’m not exactly sure what you mean, but I’m assuming that you want to be able to toggle through the fire types in the table with the press of a button. If so, you could do something like this.

ModesToCycleThrough = {"Semi", "Burst", " Auto"}
CurrentMode = ModesToCycleThrough[1] --"Semi"

function NextMode()
    Key = table.find(ModesToCycleThrough, CurrentMode) + 1 --find key of current mode plus one
    if Key > #ModesToCycleThrough then --if the key is greater than total number of keys, loop to beginning
        Key = 1
    end

    CurrentMode = ModesToCycleThrough[Key] --set current mode
end

If I am completely misunderstanding this, could you specify what you are trying to accomplish a little further?

3 Likes

Exactly! I say tables, arrays, idk. (should start using actual programming terms more often :sweat_smile:). That’s what I’m looking for, I remember looking at that function and thought about using it, but I don’t use tables often so I was a bit hesitant. Thank you, man!

1 Like