Two-weapon system not working correctly

I am currently working on a game that utilises a two-weapon system. There are pickups which you can touch in order to switch the types of guns that the player has available. The system works as such:

  1. Player touches pickup
  2. Is the player’s second weapon slot empty?
    If so, then place the weapon in the player’s primary, switching the current weapon to the second slot
    Else, just place the new weapon in the second slot, overwriting the current weapon in that slot

The script is meant to also check if any of the player’s weapons are already that specific type so as to prevent the player from holding two of the same weapon. However, when interacting with these pickups, sometimes they lock you out of the other pickups and start glitching from there, allowing the player to pick up two of the same weapon type.

The code
-- VIEWMODEL SWITCHING SCRIPT

local switchto = "pistol" -- note that this isn't the only weapon
-- I change switchto based on what viewmodel I want to show.

local part = script.Parent

part.Touched:Connect(function(hit)
	local parent = hit.Parent

	if parent and parent.Parent == workspace then
		local hum = parent:WaitForChild("Humanoid")

		if hum then
			local gun1 = parent:WaitForChild("local_values")["Gun1"]
			local gun2 = parent:WaitForChild("local_values")["Gun2"]

			if gun1.Value ~= switchto and gun2.Value ~= switchto then
				if gun2.Value == "" then

					gun2.Value = gun1.Value
					gun1.Value = switchto

				else

					gun2.Value = switchto

				end
			end
		end
	end
end)

Basically, I run them through some stringvalues and change those values, affecting the viewmodel.
While it’s not the most optimised, I am mostly just trying to get the system to work in the first place.

If needed, I can provide a file for the game simplified to just the weapon switching

If anyone knows what I could do, please reply.
Thanks.

Also, I forgot to clarify, the weapon switching and everything works if I edit the values in the character through explorer just not from the pickups.

Have you made a game on it if not could you show a image of it?

Alright, i’ll send a video of the issue, if needed i’ll also start to get the weapon system into a separate file to send a rbxl file

weapon_switchtest.rbxl (132.2 KB)
Here is a version of the game which only has the viewmodels and modules (pickups).
The main code that I believe to be broken is the pickups themselves as changing the values through the explorer works. If anyone has any ideas as to what I’m doing wrong or if I should just rework it please reply.
Thank you.

Alright, I’m dumb.
I was trying to avoid the use of remoteEvents in order to reduce lag, however trying to do so results in this bug. Sorry about making this topic, I have figured out how to fix it now.

The fixed code (includes server side)
-- SCRIPT ON PICKUP --

local RepStore = game:WaitForChild("ReplicatedStorage")
local event = RepStore:WaitForChild("Remotes"):WaitForChild("ModuleTouched")

local switchto = "pistol"

local part = script.Parent

part.Touched:Connect(function(hit)
	local parent = hit.Parent

	if parent and parent.Parent == workspace then
		local hum = parent:WaitForChild("Humanoid")

		if hum then
			event:FireClient(game.Players:GetPlayerFromCharacter(parent), switchto)
		end
	end
end)

-- SCRIPT ON CHARACTER --
local RepStore = game:GetService("ReplicatedStorage")

local gunSwitchedEvent = RepStore:WaitForChild("Remotes"):WaitForChild("ModuleTouched")
local plr = game.Players.LocalPlayer
local chr = plr.Character

local guns = chr:WaitForChild("local_values")

local gun1 = guns["Gun1"]
local gun2 = guns["Gun2"]

gunSwitchedEvent.OnClientEvent:Connect(function(switchTo)
	if gun1.Value ~= switchTo and gun2.Value ~= switchTo then
		if gun2.Value == "" then
			
			gun2.Value = gun1.Value
			gun1.Value = switchTo
			
		else
			
			gun2.Value = switchTo
			
		end
	end
end)