How can I position a model exactly in the top of a part?

Hello, I have been working on a script that will spawn pets randomly on a part and here is what I did:

local plot = workspace.PetSpawns

for i = 1, 10 do
	local pet = game.ReplicatedStorage.Pet:Clone()
	pet.Parent = workspace.Pets
	pet.Name = 'Pet'

	local x = math.random(plot.Position.X - plot.Size.X/2 , plot.Position.X + plot.Size.X/2)
	local z = math.random(plot.Position.Z - plot.Size.Z/2 , plot.Position.Z + plot.Size.Z/2)

	pet:SetPrimaryPartCFrame(CFrame.new(x,pet.PrimaryPart.Size.Y/2 + plot.Size.Y/2,z))
end

It works fine except I want to position the pet exactly on the top of the plot but this happens:
image
How do I fix this?
Thank you!

instead of setting the Y to that, position the model to the Y you want in studio, mark it down, then set it to that Y instead.

1 Like

The y should be:

plot.Position.Y + pet.PrimaryPart.Size.Y/2
1 Like

But I want it to be automatic.

Here is what happens:

local plot = workspace.PetSpawns

for i = 1, 10 do
	local pet = game.ReplicatedStorage.Pet:Clone()
	pet.Parent = workspace.Pets
	pet.Name = 'Pet'

	local x = math.random(plot.Position.X - plot.Size.X/2 , plot.Position.X + plot.Size.X/2)
	local z = math.random(plot.Position.Z - plot.Size.Z/2 , plot.Position.Z + plot.Size.Z/2)

	pet:SetPrimaryPartCFrame(CFrame.new(x,plot.Position.Y + pet.PrimaryPart.Size.Y/2,z))
end
```

So the Y is right, which means that the spawning script has an issue.

1 Like

Yes, But I can’t find the issue.

So make a separate post, saying: “Spawning script is not positioning correctly.”. Or rename the post and description.

local x = math.random(
   plot.Position.X - plot.Size.X/2 + pet.PrimaryPart.Size.X/2,
   plot.Position.X + plot.Size.X/2 - pet.PrimaryPart.Size.X/2
)

-- repeat for z
1 Like

Thank you, but for some reason I still get the same problem, It’s not placed on the top.

Is the primaryPart covering the entire model? Let me see you select the model with the blue outlines and screenshot please.

1 Like

The height of the pet should be

local y = plot.Position.Y + plot.Size.Y/2 + pet:GetExtentsSize().Y/2
1 Like

For some reason it gives me the same results.

Yes, it is covering the entire model.

Mind giving a screenshot of results of the models? I’m not sure what the issue would be.

You have to also account for how big the plot is by adding half the size of the plot on the Y axis

pet:SetPrimaryPartCFrame(CFrame.new(x,plot.Position.Y + pet.PrimaryPart.Size.Y/2  + plot.Size.Y/2 ,z))