Dropped model stops deleting after 3rd pick-up

I’m making a gun pick-up system similar to Hotline Miami


As you can see on the 3rd pick-up on the same gun, the dropped gun model stops deleting. I’m not sure what is going on.

Related Code:

-- Throwing code (Module script that runs on both server and client)
function GunBehavior.throwthegun(player : Player, GunName : StringValue, tool : Instance , Determinant : boolean)
	-- Determinant would differ between the client and server
	-- True is client, False is server

	print(GunName)

	local throwout = PhysicalDroppedGuns[GunName]:Clone()
	local basepart = throwout:FindFirstChild("BasePart")
	-- BasePart is the collision box of the dropped model due to different collision groups, think of it like a hitbox

	basepart.CollisionGroup = "NoPlayerCollisions"
	
	local prompt = basepart:FindFirstChild("Prompt")
	if prompt then
		prompt.Enabled = false
		
		if Determinant then
			prompt:Destroy()
		end
	end
	

	local bodyVelocity = Instance.new("BodyVelocity")
	bodyVelocity.MaxForce = Vector3.new(math.huge, math.huge, math.huge)
	bodyVelocity.Velocity = player.Character.HumanoidRootPart.CFrame.LookVector * 50
	bodyVelocity.Parent = basepart
	basepart.AssemblyAngularVelocity=Vector3.new(0,0,180)*basepart:GetMass()

	throwout.Parent = game.Workspace["Dropped Guns"]

	basepart.CFrame = player.Character.HumanoidRootPart.CFrame
	tool:Destroy()

	basepart.Touched:Connect(function()
		bodyVelocity:Destroy()
		if Determinant then
			throwout:Destroy()
		else
			prompt.Enabled = true
			CS:AddTag(throwout, "activeguns")
		end
	end)
end

-- Serverscript
local GrabbableWeapons = game.Workspace["Dropped Guns"]
local WeaponFolder = game.ReplicatedStorage.Tools

local DropWeaponEvent = game.ReplicatedStorage.Events.CreateDroppedWeapon
local PickUpWeaponEvent = game.ReplicatedStorage.Events.PickUpWeapon

local function PickUpTool(player, WeaponModel)
	local WeaponModelName = tostring(WeaponModel.Name)
	if not player.Backpack:FindFirstChild(WeaponModelName) and not player.Character:FindFirstChild(WeaponModelName) then
		local Weapon = WeaponFolder:FindFirstChild(WeaponModelName)
		if Weapon then
			local NewWeapon = Weapon:Clone()
			CS:AddTag(NewWeapon,guntag)
			NewWeapon.Parent = player.Backpack
			
			player.Character.Humanoid:EquipTool(NewWeapon)
			if WeaponModel:FindFirstChild("Infinite").Value == false then
				print(WeaponModel)
				WeaponModel:Destroy()
			end
		end
	end
end

local function SetUpDroppedWeapon(Tool)
	if Tool.PrimaryPart ~= nil then
		local ToolPrompt = Tool:FindFirstChild("Prompt", true)
		if ToolPrompt == nil or not ToolPrompt:IsA("ProximityPrompt") then
			ToolPrompt = ProxPrompt:Clone()
			ToolPrompt.Parent = Tool.PrimaryPart
		end
		
		
		--[[ToolPrompt.Triggered:Connect(function(player)
			PickUpTool(player, Tool)
		end)]]
	end
end

for _, Tool in CS:GetTagged(activeguns) do
	SetUpDroppedWeapon(Tool)
end

CS:GetInstanceAddedSignal(activeguns):Connect(function(Tool)
	SetUpDroppedWeapon(Tool)
end)

PickUpWeaponEvent.OnServerEvent:Connect(PickUpTool)

DroppedEvent.OnServerEvent:Connect(GunBehavior.throwthegun)

-- related Local script
PS.PromptShown:Connect(function(prompt)
	if prompt.Parent.Parent:IsA("Model") then
		local model = prompt.Parent.Parent
		if model.Highlight then
			model.Highlight.FillTransparency = 0.4
			GunInBoundary = model
		end
	end
end)

PS.PromptHidden:Connect(function(prompt)
	if not GunInBoundary then return end
	if prompt.Parent.Parent:IsA("Model") then
		local model = prompt.Parent.Parent
		if model.Highlight then
			model.Highlight.FillTransparency = 1
			task.wait(0.5)
			GunInBoundary = nil
		end
	end
end)

local InteractionCooldown = true

local DropWeaponEvent = game.ReplicatedStorage.Events.CreateDroppedWeapon
local PickUpWeaponEvent = game.ReplicatedStorage.Events.PickUpWeapon

UIS.InputBegan:Connect(function(Input, GameProcessedEvent)
	if GameProcessedEvent then return end

	if Input.UserInputType == Enum.UserInputType.MouseButton2 then
		if InteractionCooldown and GunInBoundary then
			InteractionCooldown = false
			UIS.InputEnded:Wait()
			PickUpWeaponEvent:FireServer(GunInBoundary)
			GunInBoundary = nil
			InteractionCooldown = true
		end
	end
end)
1 Like