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

How to check if every part inside a model is close enough to another?

Inside a model there are more models and every one of them has a part called "Main". Every part must be close enough to at least one other part.

for _, model in ipairs(mainModel:GetChildren()) do
	closeEnough = false
	checkFor = model:WaitForChild("Main")
	for _, model2 in ipairs(mainModel:GetChildren()) do
		checkWith = model2:WaitForChild("Main")
		if <checkFor is closer to checkWith than 4 studs> then
			closeEnough = true
		end
	end
	if not closeEnough then
		script:Destroy()
	end
end

Obviously, I also check if checkFor~=checkWith, but I didn't include that here.

What I want to know is, how to check the distance between two parts, and if there is a more efficient way to do what I'm trying to accoplish.

1 answer

✓ Accepted answer

To find the distance between two points, you just use the distance formula, which you've probably covered in math class. On roblox, though, they give us a property that does most of the math for us, called magnitude.

(b.Position - a.Position).magnitude

That will give you the distance between the points. You should be able to implement it in your code pretty easily.

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