How to detect if a tween touches a part?

I’m trying to Tween a part to its destination but I made a script that stops the tween when the part detects an objects in its path, however, the tween part just collides through the other parts.

Bar.Strike.MouseButton1Click:Connect(function()
	Ball2.Block.Touched:Connect(function(hit)
		if hit.Name == "GolfBall2" then
			BTween1:Pause()
			BTween2:Pause()
			FTween:Pause()
		end
	end)
        BTween1:Play()
        wait(1)
        BTween2:Play()
end)
```.
2 Likes

Try using magnitude to calculate whether the tween is inside the part or not.

1 Like

Why not use a raycast to detect if there are object in the way of the tween?

Touch events only fire when your tweened part (so your golfball in this case) are unanchored. Not sure if you already realised that, but it might be what you’re looking for.

1 Like

I have no idea how this works. I tried but it there is obviously something wrong with the code

local function fireLaser()
						-- Set an origin and directional vector
						local caster = workspace.GolfBallDispense
						local rayOrigin = caster.Position
						local rayDirection = Vector3.new(0, 0, 80)

						-- Build a "RaycastParams" object and cast the ray
						local raycastParams = RaycastParams.new()
						raycastParams.FilterDescendantsInstances = {caster.Parent}
						raycastParams.FilterType = Enum.RaycastFilterType.Blacklist
						local raycastResult = workspace:Raycast(rayOrigin, rayDirection, raycastParams)

						if raycastResult then
							local hitPart = raycastResult.Instance
							-- Check if the part resides in a folder, that it's fully visible, and not locked
							if hitPart.Parent == workspace.KillPartsModels["Dungeon Spikes"].SpikeRise4 then
								-- Position beam ending attachment at hit point
								caster.BeamEndAttachment.Position = Vector3.new(0, (hitPart.Position.Y-caster.Position.Y)+raycastResult.Position.Y, 0)
							end
						end
					end
					fireLaser()
1 Like

.Touched fires for both anchored and un-anchored parts

I also tried using if statements but that did not work either.

Only if one of the parts is unanchored. Plus the Touch event is very unreliable. In more precise situations or in situations where both parts are anchored, either Region3 or OverlapParams should be used.

Correction:
I just got an update regarding this post and I did want to make a correction. The Touch event is only unreliable server-side as physics are mostly done client-side. You can also use GetTouchingParts if the touching parts are unachored. When I linked OverlapParams, I was mainly referring to methods like GetPartsInParts, which works with anchored parts too. There are other methods included in that page that are more performant at the cost of exact precision of the object’s shape.

2 Likes

Try planting print statements in every crevice of that raycast code and tell me what it prints