Scripting Helpers is winding down operations and is now read-only. More info→
Ad
Log in to vote
0

Help with re-assembling a model with TweenService + Iterations and Tables of multiple parts?

Asked by
Kizonn 0
4 years ago
-- this script clones a model from a folder in the serverstorage, saves it's property values,
-- and tweens it from a random point back to it's original position to make it seem like it's being built,
-- it seems all the part's positons and orientations are mismatched due to the iteration's not matching up to the table
-- i think maybe i could add an ID to each part and reiterate until the ids match up with what's in the table, not sure if that would even fix it
-- but if anyone has a better, easier idea please tell me

-- you can test this for yourself by putting a folder in workspace and assigning it to line 19
-- make sure to put a model inside of the folder

local active = false
local tweenService = game:GetService("TweenService")
local Transparencies = {}
local Positions = {}
local Rotations = {}
local correctPartIteration = 0
local originalValues = {}
local tweenInfo = nil
local tween = nil
local randomModels = game.ServerStorage.randomModels
local randomModel = 0
local chosenModel = nil
local modelClone = nil
math.randomseed(os.time())


-- excuse the use of "or"
local setupAndSaveValues = function(model)
    for i,v in pairs(model:GetDescendants()) do
        if v:IsA("Part") or v:IsA("MeshPart") or v:IsA("UnionOperation") or v:IsA("WedgePart") or v:IsA("TrussPart") or v:IsA("CornerWedgePart") then
            v.Anchored = true

            table.insert(Transparencies, v.Transparency)
            v.Transparency = 1

            table.insert(Positions, v.Position)
            table.insert(Rotations, v.Orientation)
        end
    end
end

local buildModel = function(model)
    for i,v in pairs(model:GetChildren()) do
        if v:IsA("Part") or v:IsA("MeshPart") or v:IsA("UnionOperation") or v:IsA("WedgePart") or v:IsA("TrussPart") or v:IsA("CornerWedgePart") then
            --we dont want to use the iteration number for instances we aren't using
            correctPartIteration = correctPartIteration + 1

            -- set random position and rotation
            v.CFrame = v.CFrame * CFrame.new(math.random(3,7), math.random(3,7), math.random(3,7))
            v.CFrame = v.CFrame * CFrame.Angles(math.rad(math.random(0,360)),math.rad(math.random(0,360)),math.rad(math.random(0,360)))

            -- acutal tweening
            originalValues = {
                Transparencies[correctPartIteration],
                Positions[correctPartIteration]
            }

            tweenInfo = TweenInfo.new(2, Enum.EasingStyle.Sine, Enum.EasingDirection.Out)
            tween = tweenService:Create(v, tweenInfo, {Position = Positions[i], Transparency = Transparencies[i], Orientation = Rotations[i]})

            tween:Play()
            wait(0.1)
        end
        correctPartIteration = 0
    end
end

-- cycle for model building
while true do
    -- choose random model
    randomModel = math.random(1, #randomModels:GetChildren())
    chosenModel = randomModels:GetChildren()[randomModel]

    -- the fun stuff
    modelClone = chosenModel:Clone()

    setupAndSaveValues(modelClone)

    modelClone.Parent = game.Workspace

    buildModel(modelClone)

    wait(5)
    modelClone:Destroy()
end

Answer this question