Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
COMMUNITY QUESTION

How to position a model relative to its parent?

I've got a model made up of several nested models in ReplicatedStorage. I clone this into my workspace at a desired location, however I don't clone any of the submodels, because the player unlocks them through various upgrades. Later, once they acquire the upgrade, I want to then clone only the upgrade component into the world and place it at the proper location relative to the rest of the model.

Say for example I have this structure in ReplicatedStorage.

Foo

|_ Upgrade1 (Model)

|_ Upgrade2 (Model)

|_ PrimaryPart (Part)

This is already laid out perfectly, so if I were to clone the entire thing into my world it would look fine. However I only clone

Foo

|_ PrimaryPart

I want to add in Upgrade1 after the fact and have it appear in the proper location relative to the rest of the model, and then later on the same for Upgrade2.

How can I do this?

2 answers

If I understand correctly, this is the way most tycoons work. Parent the whole model to workspace and Parent the children to nil. Parent them back to the workspace when you upgrade and it will put them back to where they need to be.

3 comments

0

If I parent them to nil, then isn't that basically the same as destroying them? They'll get garbage collected, no? Where would I store the reference to them to make sure I can parent them back later? The other issue is that I don't even want to clone these into the workspace at all until the upgrade is purchased, because it opens the door to hacking if the upgrade is already replicated

0

But I did find my own solution, which I posted below.

0

No, they don't get collected and you can just pull them back. As far as exploiting goes, just verify the upgrade with the server. If they pull it locally only they can see it anyways.

I found an answer to my own question:

local function orientRelativeTo(referenceChild, referenceParent, workspaceChild, workspaceParent)
        -- referenceParent and referenceChild are objects that are stored in a model in ReplicatedStorage that are already positioned the way I want.
        -- workspaceParent has already been cloned into the world from referenceParent and placed in an arbitrary CFrame.
        -- The below code will figure out how to orient workspaceChild relative to workspaceParent so that it looks the same as it does in my reference model.
	
	local parentToChild = referenceParent.PrimaryPart.CFrame:ToObjectSpace(referenceChild.PrimaryPart.CFrame)
	
	workspaceChild:SetPrimaryPartCFrame(workspaceParent.PrimaryPart.CFrame * parentToChild)
end

This discussion is preserved as it was. New answers, comments, and votes are closed.