Getting Absolute Area of Multiple Models

hey there devs.

I have been working on a sandbox railway system which generates the rails with an algorithm, forming the rail piece with multiple smaller segments. What i want to do is be able to generate a part that will contain a SelectionBox that covers the area of the rail piece.

The smaller rail segments that make up the larger rail are stored as models in a folder. I’m not sure if i could use a model instead, and use a function built-in to models to get the absolute area of the model, or that doesn’t exist, and i need to use a more complex algorithm to calculate it.

here is an example of what i mean, but with the SelectionBox part hand-built:

Thankyou for any help,

  • Mezza

I’d look into model:GetBoundingBox() it returns the cframe orientation along with a vector3 size Model | Documentation - Roblox Creator Hub

Thus, to create a box to cover that segment

local function createSelection(model)
    local orientation, size = model:GetBoundingBox()
    local p = Instance.new("Part")
    p.Anchored = true
    p.CFrame = orientation
    p.Size = size
    p.CanCollide = false
    p.Transparency = 1
    local s = Instance.new("SelectionBox")
    s.Adornee = p
    s.Color3 = Color3.fromRGB(255, 255, 255)
    s.Parent = p
    p.Parent = workspace -- or where ever you want it placed.
end

this was un-tested and wrote in the forum reply, but hopefully it helps.

Thank you. Yeah, your code is pretty much what i came up with. :+1:

I’ve run into a problem. For some reason, every model’s generated part has one of its corners at Vector3(0,0,0). Is this something wrong with my implementation?

local htbx = Instance.new("Part", trckp)
local o, s = trckp:GetBoundingBox()
htbx.Name = "Hitbox"
htbx.Anchored = true
htbx.Size = s
htbx.CFrame = o
htbx.TopSurface = Enum.SurfaceType.Smooth
htbx.BottomSurface = Enum.SurfaceType.Smooth
htbx.Transparency = 1
htbx.CanCollide = false

local slcb = Instance.new("SelectionBox", htbx)
slcb.Visible = false
slcb.Adornee = htbx
slcb.Color3 = Color3.fromRGB(134, 134, 134)

Honestly I don’t see anything wrong with your implementation, but I would like to make note that assigning a parent with-in the Instance.new() can mess with performance if you’re messing with properties afterwards.

Ok thanks for letting me know that. Its wierd, one of the corners is always at 0,0,0 even though i checked that all the parts and models are in the right spot

Ok, it seems as though changing what you said about parent assignment in Instance.new actually fixed the problem, odd.

1 Like

That’s good to know, glad it helped.

1 Like