Switching System of guns using the Keybind 'Q'

Does anybody know how to make a System like Arsenal?
Like if you press Q the gun automaticaly switch?

Instead of Un-equipping, You press Letter Q to switch a gun

The Main gun will be a Sub-Machine Gun and a Secondary gun would be a Pistol

2 Likes

See ContextActionService | Roblox Creator Documentation
and UserInputService | Roblox Creator Documentation

Insert this code into a LocalScript:

local UserInputService = game:GetService("UserInputService")
local Weapons = {"SMG","Pistol"}

local CurrentWeapon = Weapons[1]

UserInputService.InputBegan:Connect(function(Input, GPE)
   if (not GPE) and Input.KeyCode == Enum.KeyCode.Q then
       local Index = table.find(Weapons, CurrentWeapon)
       if (Index + 1) > #Weapons then Index = 0 end
       CurrentWeapon = Weapons[Index + 1]
       print(CurrentWeapon)
   end
end)

Now everytime the player presses Q, the weapons will shuffle
However, to equip them, please specify if your weapons are tools or something else, and where they are located in the explorer.

Yes they are. And they are placed in a Folder inside ServerStorage

I placed them in ServerStorage to keep them from getting exploited. Someone told me a tip that exploiters cannot exploit an item that is in ServerStorage.
So i placed it there.

Great, in that case, here’s the updated tutorial that allows the player to shuffle weapons
NOTE: The weapons here will be stored in ReplicatedStorage, since Local Scripts cannot acess ServerStorage, don’t worry though, exploiters cannot exploit items here either

Step 1 - Create a Folder in StarterPlayerScripts named “WeaponShuffling”, and insert a LocalScript inside it
It should look like this:

imagem_2022-09-11_005418963

Step 2 - Inside ServerScriptService, create a Script called “EquipWeapons”
It should look like this:

imagem_2022-09-11_005819213

Step 3 - Inside ReplicatedStorage, create a Folder called “Weapons”, and insert all the weapons inside it, and then a RemoteEvent named “EquipWeaponEvent” (NOTE: The RemoteEvent is NOT inside of the Weapons folder)

It should look like this:

imagem_2022-09-11_010017450

Now let’s get into scripting! Open the LocalScript you’ve created earlier and write this:

local ReplicatedStorage = game:GetService("ReplicatedStorage")
local Players = game:GetService("Players")
local UserInputService = game:GetService("UserInputService")

local LocalPlayer = Players.LocalPlayer

local Weapons = {} -- Creates a table that will store all weapons
local CurrentWeaponIndex = 0

for i, Weapon in ReplicatedStorage.Weapons:GetChildren() do if Weapon:IsA("Tool") then table.insert(Weapons, Weapon) end end -- Checks if all weapons are tools, and inserts them into the table

local CurrentWeapon = Weapons[1].Name

local function EquipWeapon(Name)
	ReplicatedStorage.EquipWeaponEvent:FireServer(Name) -- Fires the Remote Event with the weapon name
end

UserInputService.InputBegan:Connect(function(Input, GPE) -- An input has been triggered
	if (not GPE) and Input.KeyCode == Enum.KeyCode.Q then -- If input is the key Q
		CurrentWeaponIndex += 1 -- Adds one to the current index
		
		if Weapons[CurrentWeaponIndex] == nil or CurrentWeaponIndex > #Weapons then CurrentWeaponIndex = 1 end -- Checks if either the weapon doesn't exist or the table has ran out of weapons, and if so sets the index to 1
		
		CurrentWeapon = Weapons[CurrentWeaponIndex].Name -- Updates the current weapon
		
		EquipWeapon(CurrentWeapon) -- Fires the EquipWeapon function
	end
end)

Now for the final script, open the “EquipWeapons” script in ServerScriptService, and write this:

local ReplicatedStorage = game:GetService("ReplicatedStorage")
local RemoteEvent = ReplicatedStorage.EquipWeaponEvent

RemoteEvent.OnServerEvent:Connect(function(Player, Name) -- Remote event has been fired
	if Player.Character then -- Checks if player's character has loaded
		local Weapon = ReplicatedStorage.Weapons:FindFirstChild(Name):Clone() -- Clones the weapon from ReplicatedStorage
		
		for i, Tool in pairs(Player.Character:GetChildren()) do if Tool:IsA("Tool") then Tool.Parent = Player.Backpack wait() Tool:Destroy() end end -- Checks if the player has any tools, and destroys them
		for i, Tool in pairs(Player.Backpack:GetChildren()) do if Tool:IsA("Tool") then Tool:Destroy() end end -- Same thing from above but with player's backpack
		
		Weapon.Parent = Player.Character -- Equips the weapon tool
	end
end)

Hope I helped! If you have any concerns, fell free to ask me, good luck!

Thank you so much @Martim7game , I’m using a Viewmodel now. Does it work on those?

Do you have a function to equip weapons?
For example, a ModuleScript?

Nevermind, I came back in normal. Using a Viewmodel is hard.

Can you add some Equip Animations to it?

Sure! Do you have any animations yet?

Nope, Im bad at animating. But i guess i could let me friend do it. But he’s offline tho

That’s okay, take your time!

Warn me whenever you’re ready so I can post the updated code.

Thank you very much @Martim7game

1 Like