Weapon pickup system works once, but not again?

I’m trying to make a weapon system with the new feature Proximity Prompt. Basically when you walk up to the weapon, and press E the tool will clone into your backpack. When you inventory is full, you can walk up to another weapon, equip the tool you would like to swap it with and press E. It works the first time and it clones the weapon on the floor into your inventory and removes your old weapon and puts it back on the floor. But when you press E again on the same tool which you just had, it puts the tool in your backpack back on the floor but doesn’t give you the tool back. Here’s a video of what I mean: robloxapp-20210111-2113390.wmv - Google Drive

Here’s my script, it’s quite long so I only kept in the important parts. (this is a ServerScript btw)

local weapons = game.ServerStorage.Weapons
local weaponsChildren = weapons:GetChildren()
local weaponsAmount = #weaponsChildren

local spawns = game.Workspace.WeaponSpawns

local pickUpDebounce = true

local function replaceTool(oldToolHandle, newToolHandle, name, spawnLoc, player)
	if pickUpDebounce == true then pickUpDebounce = false	
		if weapons:FindFirstChild(newToolHandle.Name) then
			player.Character.Humanoid:UnequipTools() -- Unequips tools
			game.Debris:AddItem(player.Backpack:FindFirstChild(name), 0) -- Destroys old tool in backpack
			
			wait()
			local clone = weapons[newToolHandle.Name]:Clone()
			clone.Parent = player.Backpack -- gives player new tool
			game.Debris:AddItem(newToolHandle, 0) -- destroys old gun on the floor
			
			-- Set OldTool
			local oldToolRandomY = math.random(-180, 180) -- makes new gun on the floor
			oldToolHandle.CFrame = spawnLoc.CFrame
			oldToolHandle.Orientation = Vector3.new(-90, oldToolRandomY, 0)
			oldToolHandle.Anchored = true
			oldToolHandle.Name = name
			oldToolHandle.Parent = spawnLoc
			
			local prompt = spawnLoc:FindFirstChild("ProximityPrompt")
			prompt.Enabled = true
			prompt.ObjectText = name
			
			wait(1)
			pickUpDebounce = true
		end
	end
end

local function spawnWeapons()
	for index, spawnLoc in pairs(spawns:GetChildren()) do
		if not spawnLoc:FindFirstChildOfClass("UnionOperation") then
			local randomTool = weaponsChildren[math.random(1, weaponsAmount)] -- picks a random weapon to spawn
			
			local weapon = randomTool:FindFirstChild("Handle"):Clone()
			local randomY = math.random(-180, 180)
			weapon.CFrame = spawnLoc.CFrame
			weapon.Orientation = Vector3.new(-90, randomY, 0)
			weapon.Anchored = true
			weapon.Name = randomTool.Name
			weapon.Parent = spawnLoc -- puts weapon handle in the spawn locatin
			
			local prompt = spawnLoc:FindFirstChild("ProximityPrompt")
			prompt.Enabled = true
			prompt.ObjectText = randomTool.Name -- proximity prompt stuff
			
			-- Pick Up, this is where i believe is the issues
			local debounce = true
			prompt.Triggered:Connect(function(player) -- prompt triggered
				if debounce == true then debounce = false
					local backpack = player.Backpack
					local backpackChildren = backpack:GetChildren()
					local items = #backpackChildren
					
					local oldServerWeapon
					for index, serverWeapon in pairs(weaponsChildren) do -- checks if player has weapon equipped
						if player.Character:FindFirstChild(serverWeapon.Name) then
							oldServerWeapon = player.Character:FindFirstChild(serverWeapon.Name)
						end
					end
					
					if oldServerWeapon then -- if the player has a tool equipped
						local oldToolHandle = weapons:FindFirstChild(oldServerWeapon.Name).Handle:Clone()
						local newToolHandle = weapon
						
						replaceTool(oldToolHandle, newToolHandle, oldServerWeapon.Name, spawnLoc, player)
						debounce = true
					else --if they dont have a tool equipped
						if items == 0 or items == 1 then -- check if they have inventory space
							if weapons:FindFirstChild(weapon.Name) then -- clones the tool and puts it in the backpack
								local toolClone = weapons[weapon.Name]:Clone()
								toolClone.Parent = backpack
								items = #backpackChildren

								prompt.Enabled = false
								game.Debris:AddItem(weapon, 0)
								wait(1)
								debounce = true
							end
						end
					end
					debounce = true
				end
			end)
		end
	end
end
spawnWeapons()

image

image

Let me know if you need anymore information

2 Likes