Magnitude Of Multiple Parts

Hi, I am making a system of beams connecting between HumanoidRootPart and V but I have a little problem with magnitude because my script takes magnitude of two V at once and if I’m near one of the V the beam doesn’t connect because the other V is too far.

Is there any possibility of specifying the magnitude of only one V at a time?

Short video about issue:

local Player = game:GetService("Players").LocalPlayer
local HumanoidRootPart = Player.Character:WaitForChild("HumanoidRootPart")

local Beam = Instance.new("Beam")
local Attachment0 = Instance.new("Attachment")
local Attachment1 = Instance.new("Attachment")

while true do wait()
for i, v in pairs(game.Workspace.Lights:GetDescendants()) do
		if v.Name == "BasePartS" then
			if (HumanoidRootPart.Position - v.Position).magnitude < 50 then
				Attachment0.Parent = HumanoidRootPart
				Attachment1.Parent = v
				Beam.Parent = HumanoidRootPart
				Beam.Attachment0 = Attachment0
				Beam.Attachment1 = Attachment1
				print(">50")
			else
				Beam.Attachment1 = nil
				print("<50")
			end
		end
	end
end

Why don’t you just get the magnitude of the closest part to the player, using math.huge() and once the magnitude is greater then 50, you can set the attachment to nil.

while true do wait()
	local closestMagnitude, closestPart = math.huge, nil
	for _, parts in pairs(game.Workspace.Lights:GetDescendants()) do
		if parts.Name == "BasePartS" then
		local distance = (HumanoidRootPart.Position - parts.Position).Magnitude
		if distance < closestMagnitude then -- Checks the closest magnitude from the player to part
			closestPart = parts -- Sets the 'closestPart' 
				closestMagnitude = distance -- Sets the 'closestMagnitude'
				Attachment0.Parent = HumanoidRootPart
				Attachment1.Parent = closestPart
				Beam.Parent = HumanoidRootPart
				Beam.Attachment0 = Attachment0
				Beam.Attachment1 = Attachment1
			end
			if closestMagnitude > 50 then -- Move outside of conditional statement
				Beam.Attachment1 = nil
			end
		end
	end 
end
2 Likes

Oh, I didn’t think about that. Thanks for your help :grinning: