How do I make a model spawn leveled with the ground?

  1. What do you want to achieve? Keep it simple and clear!

I want models I spawn in a script (using raycast) to spawn leveled with the ground, but it hasn’t been working so far.

  1. What is the issue? Include screenshots / videos if possible!

The models spawn like above or below the ground, and only a few models have actually worked.

  1. What solutions have you tried so far? Did you look for solutions on the Creator Hub?
    Using the same script, here are results I have gotten:

    the one above is one that worked, and the one below is one that didn’t work (these both use the same logic and script)

SOME solutions i have tried:
adding height / 2
using pivot to and bounding box / get extents to get the Y
more solutions that i dont feel like writing

Here is a snippet of my code:


local function getTrueBottom(model)
	local minY = math.huge

	for i, v in pairs(model:GetDescendants()) do
		if v:IsA("BasePart") then
			local bottom = v.Position.Y - (v.Size.Y / 2)
			if bottom < minY then
				minY = bottom
			end
		end
	end

	return minY
end

-- this part below is part of a bigger function but i just cut it out and only left the necessary parts for this problem
-- also the get true bottom function is not mine, but i decided to use it after many failed attempts of looking for solutions
				local origin = Vector3.new(math.random(-475, 475), 100, math.random(-475, 475))
				local direction = Vector3.new(0, -500, 0)
				local rc = workspace:Raycast(origin, direction)

				if rc and rc.Instance.Name == "Baseplate" then
					pos = rc.Position
					local structure = game.ServerStorage.structures[map]:FindFirstChild(strucName):Clone()
					structure.Parent = workspace
					if structure:IsA("Model") then
						local trueBottom = getTrueBottom(structure)
						local offset = pos.Y - trueBottom
						
						structure:MoveTo(pos + Vector3.new(0, offset, 0))
					else
						-- ignore this ill do it later if its not a model
					end
				end

to fix this problem i put a floor position part inside the model so you can visually adjust the floor to be wherever you want in case you want some of the model to clip into the ground

Then you can set the pivot of the model

local originalPivot = model:GetPivot()
local newPivot = originalPivot.Rotation + Vector3.new(
  originalPivot.Position.X
  floorPart.Position.Y - floorPart.Size.Y/2 -- so the bottom of the floor part = the bottom of the model when spawned
  originalPivot.Position.Z
)

model.WorldPivot = newPivot

do you think there could be any other way besides like manually editing each model? (because there are a lot of models, and I would prefer to just have it in a script in the future)

i think you can add half the height of the bounding box

local _, boundingSize = model:GetBoundingBox()
model:PivotTo(model:GetPivot() + Vector3.new(0, boundingSize.Y/2, 0))

but the pivot has to be in the center of the model, so you cant change primary part and you cant edit the pivot