Scripting Helpers is winding down operations and is now read-only. More info→
Ad
Log in to vote
0

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

Asked by 7 years ago

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

Log in to vote
1
Answered by
Perci1 4988 Trusted Moderation Voter Community Moderator
7 years ago

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.

Ad

Answer this question