How do I detect when a CFramed part touches another part

What I want to do is make a gun shoot out bullets (Lerp) too a CFrame location and detect if it hits anything to do the damage on. I’ve tried doing Instance:GetTouchingParts() but it doesn’t work.

This is my code:

local function Shoot(part, speed, length, location, plyr, target)
	local startPoint = location
	part.CanCollide = true
	part.Anchored = false
	
	part.CFrame = CFrame.new(part.Position, Vector3.new(location.x, location.y, location.z))
	
	for i = 0, length, speed do
		part.Transparency = -i
		part.CFrame = part.CFrame:Lerp(startPoint, i)
		wait()
	end
	
	part.CFrame = part.CFrame + part.CFrame.lookVector * 1
	part:Destroy()
end

I just need to make it detect when its actually hitting a player with CFrame So I can’t make them take damage.

3 Likes

Make a part touched event.

part.Touched:Connect(function(hit)
if hit.Parent:FindFirstChild("Humanoid") ~= nil then
hit.Parent.Humanoid.Health = hit.Parent.Humanoid.Health - 10 --set to the damage you want to take
end
end)

Thats the problem. The touched event doesn’t work when a part touches it by pushing the part with CFrame. It only detects when physics makes the part touch the other part. So that won’t work. Read this: Touched Event

Then just use BodyMovers.

1 Like

If you are trying to use projectiles, I would reccomend looking at the FastCast Module. The FastCast module has everything built in, runs smooth with no lag. And is super easy to set up.

Otherwise - I would use:

part.Touched:Connect(function(Hit)
if hit.Parent:FindFirstChildOfClass("Humanoid") then
-- Take Damage
end
end)
1 Like

I know this is old but BodyMovers are delayed at the start do you know why?