I want to be able to simulate the loading of a map by tween multiple parts from random positions to the parts that they are supposed to be. My code tries to accomplish that but its only 75% of the way there. My issue is trying to tween the Position and Orientation. I cannot seem to find a way to keep a log of the position of where each part is supposed to be, so when I try loading it in, the parts just move to (0,0,0) and, it doesn’t look pleasant. I’ve tried re-ordering the code, redefining the position and orientation variables, but nothing seems to be working.
local model = game.Lighting.Model:Clone()
local initialCFrames = {}
--keep track of intial positions/rotation (cframe)
for _, desc in pairs(model:GetDescendants())do
if desc:IsA("BasePart") or desc:IsA("UnionOperation") then
initialCFrames[desc] = desc.CFrame
desc.Anchored = true
desc.CanCollide = false
end
end
--spread parts out randomly +- 100 studs in each direction / rotate part ranodomly at a random angle:
for _, desc in pairs(model:GetDescendants())do
if desc:IsA("BasePart") or desc:IsA("UnionOperation") then
local offestX, offsetY, offsetZ = math.random(-100,100), math.random(-100,100), math.random(-100,100)
local angleX, angleY, angleZ = math.rad(math.random(0,360)), math.rad(math.random(0,360)), math.rad(math.random(0,360))
desc.CFrame = CFrame.new(desc.Position.X * offestX, desc.Position.Y * offsetY, desc.Position.Z * offsetZ) * CFrame.Angles(angleX, angleY, angleZ)
end
end
wait(3)
--move and load each part into the map:
for i, desc in pairs(model:GetDescendants())do
if desc:IsA("BasePart") or desc:IsA("UnionOperation") then
desc.Parent = workspace
local tweenSpeed = 1
local tween = game:GetService("TweenService"):Create(desc, TweenInfo.new(tweenSpeed), {CFrame = initialCFrames[desc]})
tween:Play()
--if the index is divisible by 2 (if it's even), wait()
--this changes how fast the map loads
--make it a higher number to go faster i.e 3, 5, 10, 20...
if i % 2 == 0 then
wait()
end
end
end