My bullets arent disappearing after touching specific part

So theres the script:

local turret = script.Parent – The turret part that rotates
local bullet = game.Workspace:FindFirstChild(“TheBullet”) – Replace with the actual bullet part name

local targetPart = game.Workspace:FindFirstChild(“targetcenterpart”) – Replace with the actual target part name
local targetAttachment = targetPart:FindFirstChild(“Attachment”) – Ensure an attachment is added to the targetPart

local spreadAngle = 0.5 – Minimal spread for more accurate shots
local bulletSpeed = 150 – Realistic bullet speed (for example, rifle bullet)
local maxForce = Vector3.new(5000, 5000, 5000) – Realistic max force for projectile movement

– Max distance before bullet is destroyed
local maxBulletDistance = 100 – Max distance the bullet can travel before being destroyed

– Function to handle bullet collision with target part or attachment
local function onBulletHit(other)
– Check if the bullet touches either the target part or the target attachment
if other.Parent == targetPart or other == targetAttachment then
– Destroy the bullet upon collision
other:Destroy() – Destroy the bullet part
end
end

while true do
if targetAttachment and bullet then
– Get the exact center of the targetPart (from the attachment’s world position)
local targetPosition = targetAttachment.WorldPosition

	-- Calculate the direction from the turret to the target center (attachment)
	local direction = (targetPosition - turret.Position).Unit

	-- Apply slight random spread to the direction to make it more realistic (minimal spread)
	local spreadX = math.random(-spreadAngle, spreadAngle) * 0.01
	local spreadY = math.random(-spreadAngle, spreadAngle) * 0.01
	local spreadZ = math.random(-spreadAngle, spreadAngle) * 0.01
	local spreadDirection = direction + Vector3.new(spreadX, spreadY, spreadZ)

	-- Rotate the turret to face the target center (attachment's world position)
	turret.CFrame = CFrame.new(turret.Position, targetPosition)

	-- Clone the bullet and set its initial position at the turret
	local clone = bullet:Clone()
	clone.Position = turret.Position
	clone.Anchored = false -- Ensure the bullet is not anchored
	clone.CanCollide = true -- Enable collision if necessary

	-- Create a BodyVelocity to control the bullet's movement
	local bodyVelocity = Instance.new("BodyVelocity")
	bodyVelocity.MaxForce = maxForce -- Apply reasonable force to keep the bullet from flying too far
	bodyVelocity.Velocity = spreadDirection * bulletSpeed -- Set the velocity direction and speed
	bodyVelocity.Parent = clone

	-- Connect the Touched event to detect collisions with the target part or attachment
	clone.Touched:Connect(function(hit)
		-- If the bullet hits the target part or the target attachment, destroy the bullet
		if hit.Parent == targetPart or hit == targetAttachment then
			clone:Destroy() -- Destroy the bullet part
		end
	end)

	-- Parent the bullet to the workspace
	clone.Parent = game.Workspace

	-- Add the bullet to Debris to automatically clean up after 5 seconds
	game:GetService("Debris"):AddItem(clone, 5)

	-- Prevent the bullet from flying too far by checking its distance
	local startPosition = clone.Position
	spawn(function()
		while clone and clone.Parent do
			-- Check the distance between the bullet and its starting position
			if (clone.Position - startPosition).Magnitude > maxBulletDistance then
				clone:Destroy() -- Destroy the bullet if it travels too far
			end
			wait(0.1)
		end
	end)
end

wait(0.1)

end

I can see bullets are touching the targetpart but they arent disappearing for some reason. Yes I tried printing through output if targetpart was touched and when random item is touched. And in result it only says when random part is touched (including the targetcenterpart) but not the targetpart itself. What do I do?

Can you specify where in the script the bullets are supposed to disappear after touching this ‘specific part?’

Yes, after touching the “targerPart” / ‘targerAttachment’

I’m sorry, but I don’t think I am experienced enough to help you, but I also believe there’s not enough information here for people to help you.

Sorry for the false hope :frowning: