Help with :SetPrimaryPartCFrame with models

Hello!
I’m working on plot selecting for players, where they can spawn owned warehouses. But, I’ve run into a problem with spawning certain warehouses. My small warehouse is experiencing no problems at all, although when it comes to my Medium and Large Warehouse, this is where my problems occur. Here’s a picture of how they are spawning, and the script for spawning them in.

Medium and Large Warehouse

Remotes.SpawnPlot.OnServerEvent:Connect(function(Player, Type, Plot)

	print("[SERVER] Spawning ".. Player.Name .. "'s " .. Type .. " at Plot   " .. Plot .. "    .")
	
	local PLAYERDATA 	= ReplicatedStorage:WaitForChild('PlayerData'):FindFirstChild(Player.Name)
	local PossibleWarehouses 	= game.ServerStorage:WaitForChild("Warehouses")
	local Warehouse 			= PossibleWarehouses:FindFirstChild(Type)
	local PossibleCFrames		= game.ServerStorage:WaitForChild("PlotCFrames")
	local PlotSpawn				= workspace.SpawnedPlots:WaitForChild(tonumber(Plot))

	if Type and Warehouse and PlotSpawn.Owner.Value == "Nobody" then

		local CloningWarehouse = Warehouse:Clone()
		CloningWarehouse.Parent = PlotSpawn
		CloningWarehouse.Name = "" .. Player.Name .. "" --"'s ".. Type .. ""
		CloningWarehouse.Owner.Value = tostring(Player.Name)
		PlotSpawn.Owner.Value = tostring(Player.Name)
		CloningWarehouse:SetPrimaryPartCFrame(CFrame.new(PossibleCFrames:WaitForChild(Plot).Value))
		
		local SP = Instance.new("NumberValue", PLAYERDATA)
		SP.Name = "SelectedPlot"
		SP.Value = Plot

	end
end)

If there’s anything I can do to help you, please let me know! Thank you if you’re able to help me out! If not, no worries have a great rest of your day!

So what is the problem? You seem to have forgot to show us more about the problem, as the “my Medium and Large Warehouse, this is where my problems occur” does not tell us anything of what the problem is.

But since you used SetPrimaryPartCFrame in the topic, then I guess you have some error output from that line.

Perhaps you simply forgot to set the property PrimaryPart for your medium & large warehouse models?

Or could it be something about X,Y,Z position?

I understand, my apologies for not explaining further details about my problem. I have a primary part set on the warehouses. In the photo I attached, my warehouses are halfway down in the baseplate, while yes I can change the X, Y, Z position of the primary part for that to appear facing the right way, but that isn’t what I’m trying to fix at the moment. So, what I’m trying to achieve is for the warehouses to spawn on the baseplate and not halfway in the baseplate.

Also, if it helps, my plot CFrame values are Vector3 Values.

As I read the code you made, then I would think, that your medium & large warehouses’ PrimaryPart block, probably is at a different relative location (Y axis), when compared to your small warehouse. - I.e. they are not all at the same “floor”-position. - Or maybe even the PrimaryPart blocks have different sizes for the different warehouses?

Or perhaps your PossibleCFrames “array” of vectors does not have the same Y-value?

Just set the primary part of your houses to the floor, do a clone of it, parent it to workspace and do the setprimarypartcframe with that clone as the value, i think this could solve your issue. Feel free to ask questions if you didn’t understand :slight_smile:

Also remember, that a PrimaryPart block does not even have to be visible.

Here’s a picture of what my large warehouse does when it is spawned.

https://gyazo.com/291ad5ecc6a5fc44b72130a2d45e6b37

Is there a way to fix this without changing the values for the plot Vector3 values? I’m planning on making a large map that can be explored, but I’m wanting to keep the plot locations in the same location. I hope what I want to achieve makes sense.

So your CFrame is marked as the position of the baseplate if its halfway in? just multiply it by the baseplate size in Y direction divided by 2 in a cframe? (CFrame.new(0, BasePlate.Size.Y/2, 0))

Here’s an example of a Vector3 value that works for the small warehouse, but the medium and large, they spawn half inside the baseplate. An example of the Vector3 value is: 74.515, 8.05, -228.065 the large warehouse Primary Part dimensions are: 82.2, 47.9, 168.1 if this helps at all.

oh so you have to account for the size of the model instead to do setprimarypartcframe or manually find different vectors that work instead?

My script randomly selects 1 of the 12 possible locations and then spawns at the corresponding plot.

well you’d have to account for model size without changing vector3 values manually. you can use :GetBoundingBox() on the model which returns a CFrame and Size or make an invisible uncollidable block that represents the entirety of the model then you just do cframe math.

You can try this:

BoundingCFrame:PointToWorldSpace(CFrame.new(0, BoundingSize.Y/2, 0))

just index the position property of the cframe if you want the vector3 or just use the cframe.

Can you give me an example of how to properly use this? I don’t quite understand how I could use this properly.

local CloningWarehouse = Warehouse:Clone()
local cframe, size = CloningWarehouse:GetBoundingBox()
local newCFrame = BasePlate :PointToWorldSpace(CFrame.new(0, size.Y/2, 0))
CloningWarehouse:SetPrimaryPartCFrame(newCFrame)

Would I change the “BasePlate” to the hitbox (Primary Part) that I have for the warehouse?

I’m not sure what you call your plot area, it’s supposed to be replaced with whatever you want to place it on without it being inside the plot, so I believe it’s your plot spawn.

I’m sorry I’m being so picky/needy but, is there a way I can use my Vector3 values to get this to work properly? I just tried to edit the newCFrame to correspond with the plot Vector3 value but that did not work. Is there any way I can get this to work properly with Vector3 values?

Hello!
I was able to get around to this and it worked! Although, my buildings are rotated in different directions. How could I fix this?

1 Like

If you are still using SetPrimaryPartCFrame, make sure your primary parts are all oriented the same way relative to their respective warehouses (e.g. the front surface of the primary part is facing the front door of the warehouse). Then you can make sure the CFrame values for the plots are correct.