Parts not getting destroyed

Hello, I’m making a tool that shoots multiple parts(50) in the direction of the mouse using a tween and the parts are supposed to be destroyed after either 2 seconds via game.Debris:add() have pasted or they hit an something that is not the player via Destroy:().

The issue I’m having is that the parts don’t get destroyed via Destroy:() after touching something. I don’t understand why.

LocalScript:

local player = PlayerService.LocalPlayer
local character = player.Character

if not character or not character.Parent then
	character = player.CharacterAdded:Wait()
end

local humanoid = character:WaitForChild("Humanoid")

local PelletBarrageTool = script.Parent

local PlayerMouse = player:GetMouse()

local Tool = "PelletBarrage"

local Active = false

PelletBarrageTool.Activated:Connect(function()
	if not Active then
		
		for x = 1,50 do
			ToolRemoteEvent:FireServer(
				Tool,
				PlayerMouse.Hit.p
			)
			wait(0.05)
		end
	end
end)

Script:

local ReplicatedStorage = game:GetService("ReplicatedStorage")
local TweenService = game:GetService("TweenService")

local ToolRemoteEvent = ReplicatedStorage.Remotes.RemoteEvents.ToolRemote

ToolRemoteEvent.OnServerEvent:Connect(function(
	player, 
	Tool, 
	MouseHit
)
	local character = player.Character
	local RootPart = character:FindFirstChild("HumanoidRootPart")
	
	local humanoid = character:FindFirstChild("Humanoid")
	
	local Pellet = Instance.new("Part", workspace.PlayerObjects)
	Pellet.Name = player.Name.."'s Pellet"
	Pellet.CanCollide = false
	Pellet.Anchored = true
	Pellet.Shape = "Ball"
	Pellet.BrickColor = BrickColor.new("White")
	Pellet.Size = Vector3.new(0.5, 0.5, 0.5)
	
	Pellet.CFrame = RootPart.CFrame * CFrame.new(math.random(-6,6),math.random(4, 8),math.random(1,3))
	Pellet.CFrame = CFrame.new(Pellet.Position, MouseHit)
	
	local Distance = (Pellet.Position - MouseHit).Magnitude
	local TimeSpeed = Distance / 50
	
	local PelletLaunchTweenInfo = TweenInfo.new(TimeSpeed)
	local PelletLaunchTween = TweenService:Create(
		Pellet,
		PelletLaunchTweenInfo,
		{
			Position = MouseHit
		}
	)
	
	PelletLaunchTween:Play()
	
	game.Debris:AddItem(Pellet, 2)
	
	Pellet.Touched:Connect(function(Hit)
		if Hit.Name ~= player.Name.."'s Pellet" then
			if not (Hit.Parent:FindFirstChild("Humanoid") == humanoid) then
				if game.Players:GetPlayerFromCharacter(Hit.Parent) then
					
					local HitHumanoid = Hit.Parent:FindFirstChild("Humanoid")
					HitHumanoid:TakeDamage(1)
				elseif Hit.Parent:FindFirstChild("Humanoid") then
					
					local HitHumanoid = Hit.Parent:FindFirstChild("Humanoid")
					HitHumanoid:TakeDamage(1)
				end
				Pellet:Destroy()
			end
		end
	end)
end)

Example:

It hits the pellets, and the rest of the code doesn’t run, therefore it doesn’t get destroyed.

that just makes sure that it hitting the pellet doesn’t destroy it, I know that it runs when hitting other object because it damages the dummy.

Yes, but I’m saying that it hits the pellets first and stops moving due to the mouse’s hit being onto the other pellet. To fix this, simply add

PlayerMouse.TargetFilter = workspace.PlayerObjects

inside of the client script.

Oh, I see, I did that and it’s now destroying them when hitting a dummy but only being destroyed sometimes when hitting anything else.

I believe the Baseplate has “CanTouch” off.

I just checked, all the workspace parts have can touch on

This is weird then. If the Baseplate has CanTouch checked, then the pellet should be able to touch it. So, are you absolutely sure the Baseplate has CanTouch on?
image

yeah, i checked and tried turning it on and off again but nothing happened

Are there any errors in the console? Could you try moving the pellet:Destroy() out of the second if statement?

if Hit.Name ~= player.Name.."'s Pellet" then
			if not (Hit.Parent:FindFirstChild("Humanoid") == humanoid) then
				if game.Players:GetPlayerFromCharacter(Hit.Parent) then
					
					local HitHumanoid = Hit.Parent:FindFirstChild("Humanoid")
					HitHumanoid:TakeDamage(1)
				elseif Hit.Parent:FindFirstChild("Humanoid") then
					
					local HitHumanoid = Hit.Parent:FindFirstChild("Humanoid")
					HitHumanoid:TakeDamage(1)
				end
			end
			
			Pellet:Destroy()
		end

i did, it didn’t change anything

did the console have any errors? If it didn’t, try printing everything that the pellet hit.

	Pellet.Touched:Connect(function(Hit)
		print(Hit.Name, Hit)
		if Hit.Name ~= player.Name.."'s Pellet" then
			if not (Hit.Parent:FindFirstChild("Humanoid") == humanoid) then
				if game.Players:GetPlayerFromCharacter(Hit.Parent) then
					
					local HitHumanoid = Hit.Parent:FindFirstChild("Humanoid")
					HitHumanoid:TakeDamage(1)
				elseif Hit.Parent:FindFirstChild("Humanoid") then
					
					local HitHumanoid = Hit.Parent:FindFirstChild("Humanoid")
					HitHumanoid:TakeDamage(1)
				end
			end
			
			Pellet:Destroy()
		end
	end)

it doesnt print anything when hitting the baseplate, but CanTouch is on

wait does :FindFirstChild() look through the children of the children?

No, FindFirstChild only searches for the children of the object, not the descendants.

I have to go to bed now, stayed up late enough, and have school in a few hours, so I will try and help you later on (12-14 hours) if you are still having difficulties.

.Touched does not work with anchored object, so you will want to do it like that:

Weld the part you want to be touched with an anchored object via weld constraint and then unanchor the part you want to be touched

1 Like

Touched does work with an anchored object so idk where you got that from.

Go check the developer hub, it’s written there


The pellets are unanchored.

Screenshot_2022-09-23-01-02-29-600-edit_com.android.chrome (1)