SetPrimaryPartCFrame sets CFrame of PrimaryPart, but not other parts in model

I’m having some trouble with my module, but first, let me explain how my game works.

Game

I’m creating a backrooms game and instead of generating a map from scratch, I built different ‘chunks’ and then a script positions those chunks.
Each chunk has a number of root parts, those root parts are at every passage of a chunk.

I set the PrimaryPart of the chunk to one of the roots (randomly selected), and then I do

Chunk:SetPrimaryPartCFrame(RootPartOfOtherChunk.CFrame)

Each root part has its own while loop that checks if a character gets close to it, if close enough, a new chunk will be spawned.

The problem

The first few chunks spawn in as expected but after 3-to 4 chunks the root part of the new chunk is positioned correctly inside of the other chunk’s root part but without the actual chunk.

In other words, the model’s PrimaryPart is set, the PrimaryPart is positioned correctly, but not the model

This is the part of my code that selects a root, clones a chunk, and set’s the PrimaryPartCFrame.

local function SelectRandomRoot(RootFolder)
	local AllRoots = {}
	
	for _, PossibleRoot in pairs(RootFolder:GetChildren()) do
		if (PossibleRoot:IsA("BasePart")) and string.lower( string.sub(PossibleRoot.Name, 1, 4)  ) == "root" then
			table.insert(AllRoots, PossibleRoot)
		end
	end
	
	local Root = AllRoots[math.random(1, #AllRoots)]
	
	return Root
end
function BackMaps:NewPiece(OldRoot, Level)
	local Settings = self.Settings
	
	-- [Chunk is being selected here, and its variable name is "RandomPiece" but that's unnecessary to show in this post.]
	
	-- Cloning into the workspace
	local NewRandomPiece = RandomPiece:Clone()
	NewRandomPiece.Parent = Settings.WorkspacePiecesDirectory
	

    -- Selects a random root, SelectRandomRoot function is shown above.
	local NewPieceRoot = SelectRandomRoot(RandomPiece.Roots)
	if NewPieceRoot then
		NewRandomPiece.PrimaryPart = NewPieceRoot

		-- Set the PrimaryPartCFrame (* 180 degrees for correct orientation)
		NewRandomPiece:SetPrimaryPartCFrame(OldRoot.CFrame * CFrame.Angles(0, math.rad(180), 0))
	
		return NewRandomPiece, NewPieceRoot
	end
end




I hope my explanation wasn’t too bad.

I have been trying to figure this out for a few days now, but I can’t find an answer.
Any ideas on what this could be?

2 Likes

My first guess is that the rotation is messed up and the next chunk spawns literally inside of the previous chunk.

Use explorer to see where the new chunk is and where are its parts. It’s not possible that it randomly moves the primary part without the rest of the model.

1 Like

The rotation is correct, the offsets between PrimaryPart and the rest of the model have changed.

Another thing I noticed is that the selection box on hover is also messed up, some times it’s completely offset of the model, and some times it disappears completely.

Could you post a picture of the explorer with the models of a broken and working chunk? I bet something is different between the chunks. You may also want to check the properties of the parts in the non-working chunks since those can have an effect.

I’ve had issues like this before and it usually just came down to making minute changes until it worked.

1 Like

I bet this is probably a really easy fix but I have no clue what the problem is.
Right now I’m noticing something weird.


These are the walls of a chunk ^

These are all of the roots ^ (don’t worry about colliding chunks, I set up a debugging place with as little confusing code as possible so it’s easier to debug)
This is weird, I’m pretty sure nothing else is changing the positions of the root, in-studio the positions are normal.


This is the explorer of a chunk, there are no differences between working and broken chunks.
image

I’ve also looked at the properties, those are fine too.
For testing purposes I also made it use 1 single chunk for the entire map, it’s weird that one copy works, and another doesn’t.

Roblox can sometimes be weird which might be some of your issue.

What I think it might be, is a bug with changing the primary part on the fly. You may want to try removing the entire random function and just having it do say the north root as a test.

1 Like

Already did that, the problem stayed.
Instead of spending another few hours on debugging, I’ll probably now spend that time on a rewrite of the chunk placement.

Hmm…
In that case it has to be an issue with the specific chunks, or at least in my mind. I doubt the script is at fault.

1 Like

I don’t know what’s causing it, it’s so weird, day 1 it worked flawlessly, day 2 it broke.
This is a video taken when it did work.

That looks really cool.

If it did work, then you must have changed something. Retrace your steps!

1 Like

I might be reading your code wrong here, but I think your clone chunk’s NewRandomPiece primary part is being set to the old chunk’s RandomPiece root.

I believe NewPieceRoot should = SelectRandomRoot(NewRandomPiece.Roots)

You should probably use the superseded method PivotTo :slightly_smiling_face:
Instead of changing the PrimaryPart’s CFrame you’re now changing the Pivot’s CFrame of your model.