Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
COMMUNITY QUESTION

Part's Magnitude Including Size?

see what ever parts is close to base INCLUDING SIZE like if part.Size = Vector3.new(9,20,2) if part magnitude is close to part in that size,or like if u making a kamehameha and the beam goes forward it dont only check the middle point if the part extends it gets the front to bck magnitude and runs script

1 answer

Haven't been able to test it but this should theoretically work, assuming that the parts are rectangular

FYI I only answered this because I wanted to know how to do this also, and I don't think anybody would have ever answered this question because of the request site thing.

local lowestMagnitude = 10000
local partWithMagnitude = nil
local part = game.Workspace.Part

local function goThroughParts(v)
	if #v:GetChildren() ~= 0 then
		for i, v2 in pairs(v:GetChildren()) do
			goThroughParts(v2)
		end
	end
	if v:IsA("Part") then -- This is where it actually does the comparison based on size
		local p = v.Position
		local s = v.Size
		local function getClosest(i, j, k, l)
			if (i + j > k + l and i - j < k + l) or 
				(i + j > k - l and i - j > k - l) then
				return 0, 0
			elseif(i + j < k - l) then
				return i + j, k - l
			elseif(i - j > k + l) then
				return i - j, k + l
			end
		end
		local closestX, closestPartX = getClosest(p.X, s.X / 2, part.Position.X, part.Size.X / 2)
		local closestY, closestPartY = getClosest(p.Y, s.Y / 2, part.Position.Y, part.Size.Y / 2)
		local closestZ, closestPartZ = getClosest(p.Z, s.Z / 2, part.Position.Z, part.Size.Z / 2)
		local magnitude = (Vector3.new(closestX, closestY, closestZ) - Vector3.new(closestPartX, closestPartY, closestPartZ)).magnitude
		if magnitude > lowestMagnitude then
			lowestMagnitude = magnitude
			partWithMagnitude = v
		end
	end
end
for i,v in pairs(workspace:GetChildren()) do
	goThroughParts(v)
end
print(partWithMagnitude)

3 comments

0

on line 27 it says vector3 expected got number

0

Added 2 parenthesis to code so it's (Vector3 - Vector3).magnitude

0

when i run the script it keeps on printing nil

This discussion is preserved as it was. New answers, comments, and votes are closed.