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:
- Player touches pickup
- 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.