How can i detect if a part is intersecting with two points

so i have two parts with attachments:


when activated it clones the second part :

how can i detect if a part intersects with the beam?
(attachments lightning and lightning2)

You should probably move this into Scripting Support.

I think raycasting I’m not totally sure though

im a noob idk how to raycast and parts dont work because it goes over 2048 studs

watch this video

1 Like

This is a script that I made:

local attachmentA,attachmentB --Set these to the lightning start and end point attachments

function detect()
 local positionA,positionB = attachmentA.CFrame.p,attachmentB.CFrame.p
 local distance = (positionA-positionB).Magnitude
 local facing = CFrame.new(positionA,positionB)
 local params = RaycastParams.new()
 params.FilterType = Enum.RaycastFilterType.Blacklist
 params.FilterDescendantsInstances = {} --Table of parts to ignore eg. the one(s) holding the attachments
 local result = workspace:Raycast(positionA,facing.LookVector*distance,params)
 if result then
  return result.Instance
 end
end

i don’t want to blacklist any parts because soon it would just check for an attribute called “health”
didnt seem to work:


result:

it’s my alt acc used for developing

Well, you don’t have to include blacklisted parts. Anything that is not in the table is able to be detected. Also, the result is an object itself with the properties Position (the exact position that was hit), and Instance (the object that was hit). To get the actual object that was hit, at the end of the code, write “print(detect())”

Edit:
Also, at the line where AttachmentA and AttachmentB are declared, you need to provide AttachmentB. This is how its supposed to be formatted:

local attachmentA,attachmentB = [first attachment],[second attachment]

1 Like

STILL DOSENT WORK
MY CODE:

local tool = script.Parent
local handle = tool.Handle
local grip = handle.Grip
local fire = tool.shoot
local zap = tool.zap:WaitForChild("lightning2")
spawnpos = tool.zap.Spawnpos.WorldPosition
tool.Grip = grip.CFrame
local function onFire(player,targetPos)
	local lightning = game.ReplicatedStorage.projectiles:WaitForChild("lightning")
	local clone = lightning:Clone()
	local beam = clone:WaitForChild("zap")
	local attatchment = clone:WaitForChild("lightning")
	beam.Attachment0 = zap
	beam.Attachment1 = attatchment
	clone.Position = targetPos
	clone.Parent = workspace.projectiles
	tool.Enabled = false
	local attachmentA = zap
	local attachmentB = attatchment --Set these to the lightning start and end point attachments

	local function detect()
		local positionA,positionB = attachmentA.CFrame.p,attachmentB.CFrame.p
		local distance = (positionA-positionB).Magnitude
		local facing = CFrame.new(positionA,positionB)
		local params = RaycastParams.new()
		params.FilterType = Enum.RaycastFilterType.Blacklist
		params.FilterDescendantsInstances = {tool:WaitForChild("zap"),clone} --Table of parts to ignore eg. the one(s) holding the attachments
		local result = workspace:Raycast(positionA,facing.LookVector*distance,params)	
		if result then
			return result.Instance
		end
	end
	for i = 1,10 do
		wait()
		detect()
	end
	print(detect())
   	clone:Destroy()
	tool.Enabled = true
end 
fire.OnServerEvent:Connect(onFire)

What doesn’t work here? For testing, I would create a while loop printing detect(). Also, detect() returns an instance, so it is practical to assign it to a variable.

correct code:
local function detect()
local StartPoint = clone
local EndPoint = tool:WaitForChild(“zap”)
local result = workspace:Raycast(StartPoint.Position,EndPoint.Position - StartPoint.Position)
if result then
return result.Instance
end
end