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

How would I move a part relative to another part?

I'm trying to create a model-moving tool that cframes each part separately, but I can't figure out how I move a model in relativity to the primary part.

For example, these are too parts (One above and one below)...

?

but, I move the bottom half downwards...

?

v

I am trying to figure out what I need to do to move the top half to the bottom half to complete the diamond by only using their past positions as a guide.

So far, I've only figured out that I need to use:

Part1.CFrame * Part2.CFrame

But, I can't wrap my head around the math needed. If anyone know how to do this or has any advice, let me know.

7 comments

0

The question marks are parts of the Diamond. The first question mark is the full Diamond, the second is the top half of it, they were censored as they are not actual keys.

0

Welds?

0

No, CFrame, as mentioned in the post like 3 times.

0

^With that, all other parts will move relative to the PrimaryPart

0

Again Witchest, do you want to recreate SetPrimaryPartCFrame with ungrouped parts? I could answer that. Its hard to answer weird thought experiments.

0

Yes, cabbler, that's exactly what I want!

2 answers

repeat wait() until game.Players.LocalPlayer
repeat wait() until game.Players.LocalPlayer.Character:IsDescendantOf(workspace)
----------------------------------------------------------------------------------------------------------------

--CONSTANTS;

local Player = game.Players:WaitForChild(game.Players.LocalPlayer.Name)

local Mouse = Player:GetMouse()
local MouseHeld = false 

local CurrentModel = nil 

--CLICK FUNCTIONS;

function CF_ONCLICK()
	if not MouseHeld and Mouse.Target then 
		MouseHeld = true 
		if Mouse.Target:IsA('BasePart') then 
			CF_FINDMODEL(Mouse.Target)
			repeat wait() until CurrentModel ~= nil 
			CF_DRAGMODEL(CurrentModel)
		end
	end
end

function CF_ONRELEASE()
	if Mouse.Held then 
		Mouse.Held = false 
		CurrentModel = nil 
	end
end

function CF_FINDMODEL(part)
	if part then 
		if part.Parent:IsA('Model') then -- assuming that 'part' is only in one model
			CurrentModel = part.Parent
		end
	end
end

function CF_DRAGMODEL(model)
	while MouseHeld do 
		if model:IsA('Model') then 
			model:MoveTo(Mouse.Hit.p)
		end
	wait()
	end
end

Mouse.Button1Down:connect(CF_ONCLICK)
Mouse.Button1Up:connect(CF_ONRELEASE)

2 comments

0

Don't know if this will work, wrote it based on my knowledge. Comment if you came across any problems.

Anyone else should use model.SetPrimaryPartCFrame. but apparently you do want to 'recreate' it

You just need to maintain all original offsets from the PrimaryPart. Best way to get offsets is toObjectSpace; originCF:toObjectSpace(targetCF). All math is behind the scenes.

With two parts:

local goal = CFrame.new() --change
part1.CFrame = goal * part0.CFrame:toObjectSpace(part1.CFrame)
part0.CFrame = goal --pseudo primary part

It's possible to move any number of parts by repeating the part1 line except with part2 etc. So, obviously put that in a part-loop, wrap with a function, and you have a generic model-mover!

--take array of parts, the primary part, and a goal cframe
local function SetPrimaryPartCFrame(parts,ppart,cf)
	local ppcf = ppart.CFrame
	for i=1,#parts do
		local v = parts[i]
		v.CFrame = cf * ppcf:toObjectSpace(v.CFrame)
	end
	ppart.CFrame=cf
end

hope you understand better

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