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?