Need Help Creating Weapon Switching System Using One Input

Hello, everyone!

I’m currently rewriting Redshift Arena’s codebase, and overall redoing the game from the ground up.

I’m at a bit of an impasse currently, involving wanting to use one input function to handle all 8 weapon hotbars.

The main thing that is halting this is the simple fact that Enum.KeyCode isn’t “One” or “Two” it’s “Enum.KeyCode.One” or “Enum.KeyCode.Two”

This is a problem since in my hotbar table, the hotbars are listed as “One, Two, Three,” and so fourth.

Here’s the code so far:


if inputObject.UserInputType == Enum.UserInputType.Keyboard then
					if inputObject.KeyCode == Enum.KeyCode[WeaponStats.WeaponHotBar[inputObject.Keycode]] then
						equipGun(WeaponStats.WeaponHotBar[inputObject.KeyCode])
					end
end

And here is how my table is structured (within a ModuleScript.)

WeaponSettings.WeaponHotBar = {
	One = {
		"Pistol",

	},
	Two = {
		"AssaultRifle",

	},
	Three = {
		"Shotgun",

	},
	Four = {
		"PlasmaRifle"
	},
	Five = {
		"RocketLauncher",

	},
	Six = {
		"SniperRifle",

	},
	Seven = {
		"Minigun",

	},
	Eight = {
		"PlasmaCannon",

	},
	
}

I’d prefer to use a single input for this, rather than having to do multiple if / else statements for each gun in the hotbar.

Should also be noted that I intend on having multiple guns share the same hotbar slot. I.e. Pistol & Duel Pistol would share slot one.

local function splitWeaponKeyCode(inputString)
    local splitString = string.split(tostring(inputString),".")
    return splitString[3]
end

if inputObject.UserInputType == Enum.UserInputType.Keyboard then
    if inputObject.KeyCode == Enum.KeyCode[WeaponStats.WeaponHotBar[splitWeaponKeyCode(input.KeyCode)]] then
    equipGun(WeaponStats.WeaponHotBar[inputObject.KeyCode])
    end
end

This is a fairly specific solution, and I think it’ll work for your script.

2 Likes

Thank you so much for this!

I had to make a few minor changes, but other than that, this works flawlessly!! :slight_smile:

1 Like