Getting Original CFrame and applying it back later in the script

So how would I go about getting the original coordinates of where each part in a model (Tree) is, then making the coordinates of where the parts in the tree has ended up back to their original coordinates.

Here is what I have right now, currently I am getting all the coordinates through a for loop, but all the parts in the tree are going to the exact same place not their original place.

for index, v in pairs(Tree:GetDescendants()) do
			if not v:IsA("BasePart") then
				continue
			end

			task.spawn(function()
				v.CFrame = originCFrame
			end)
		end

This should work:

local OriginalPositions = {}

for index, v in pairs(Tree:GetDescendants()) do
	if not v:IsA("BasePart") then
		OriginalPositions[v] = v.CFrame.Position -- Store the original position in the table
	end
end

All this will do is loop through all the descendants of the Tree and add their positions to a table for later use.
If you want to then reset them simply add this to the script:

for part, OriginalPosition in pairs(OriginalPositions) do
	task.spawn(function()
		part.CFrame = CFrame.new(OriginalPosition) -- Reset the positions
	end)
end

Hope this helps!

2 Likes

Well, isn’t it better storing CFrame in the dictionary instead of Position? For instance, if the treebranch is rotated this will only set the position correctly, but orientation to (0,0,0)

2 Likes

fair point, my bad, should have remembered about that

1 Like
local OriginalCFrames = {}

for _, v in pairs(Tree:GetDescendants()) do
	if v:IsA("BasePart") then
		OriginalCFrames[v] = v.CFrame -- Store the original CFrame in the table
	end
end

for part, OriginalCFrame  in pairs(OriginalCFrames) do
	task.spawn(function()
		part.CFrame = OriginalCFrame -- Reset the CFrame
	end)
end
1 Like

Exactly what I was about to type!
Also wasn’t it “if not v:IsA(“BasePart”) then”?
Either way, shouldn’t be an issue, it’s an easy fix.

Yeah I changed it haha, it’s working now, thank you for both of your help!

He used if not v:IsA(“BasePart”) then continue end.

What continue does is it skips the current lap of the loop, simply going to the next one.

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.