Script doesn't detect all Touch events?

Hi, everyone.
I am starting to make a gun script. My issue is, my script doesn’t detect when the bullet hits a wall.
However, it does detect when it hits my character. I have no idea why it does this. Anyone know why?

while true do	
	wait(1)

	local p1 = script.Parent.Position
	local p2 = workspace.pos2.Position
	
	local bolt = Instance.new("Part", workspace)
	bolt.Name = "bolt"
	bolt.Anchored = true
	bolt.CanCollide = false
	bolt.Material = Enum.Material.Neon
	bolt.BrickColor = BrickColor.new("Bright blue")
	bolt.Size = Vector3.new(0.125, 0.075, 2.125)

	bolt.CFrame = CFrame.new(p1, p2)
	
	local speed = (p1 - p2).magnitude / 50
	
	local ts = game:GetService("TweenService")
	local tween = ts:Create(bolt, TweenInfo.new(speed, Enum.EasingStyle.Linear), {Position = workspace.pos2.Position})
	tween:Play()
	
	bolt.Touched:Connect(function(hit)
		print("Hit: " .. hit.Name)
		if hit ~= script.Parent then
			bolt:Destroy()
		end
	end)
	
	spawn(function() --rid stray bullets
		wait(10)
		if bolt then
			bolt:Destroy()
		end
	end)

end
1 Like

This is odd, please could you show me your workspace?

This way I can understand all of the parenting and help you get to the core of this issue. :smile:

https://gyazo.com/b52a9214d6e6bf2696d764e9cbe21b35

This is occurring because you are tweening an anchored part to another anchored part. Touched falls under the physics pipeline and only detects for parts that intersect by cause of physics.

The player is physically simulated because they are moving and unanchored therefore the touched event will detect for them. The wall is static and everything from the wall to the bullet is anchored, so no physics are being simulated on them.

Unanchor the bullet and use a body mover (either VectorForce or BodyForce, or whatever you want so long as it’s a force).

2 Likes

You could also cast a ray before firing the bullet to see what it’ll hit if its not a player.

I think it should be:
if hit.Name ~= script.Parent.Name then

Perhaps you could make the Bullet use a look vector and fire it constantly so when a wall approaches it knows to disappear instead of relying on a touch script.