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

How to Cframe a whole model?

Asked by 6 years ago
Edited 6 years ago

Hello,

So after getting some help on how to actually Cframe a brick, I would like to know how I would go about making it so that whatever model that brick is in, it will move the all the bricks in that model along with it.

Thanks,

Anthony

Here is the Cframe script

local detector = game.Workspace.Part.ClickDetector; -- Variable for the ClickDetector
local tween_service = game:GetService("TweenService") -- Variable for the TweenService
local curtain = workspace:WaitForChild("Curtain"); -- Variable for the Curtain
local old_pos = curtain.CFrame; -- Variable for the Curtain's CFrame that we will be position the Curtain by.
local first = false; -- Variable to keep track of whether the part's supposed to go up or down.
local info = TweenInfo.new(1, Enum.EasingStyle.Linear, Enum.EasingDirection.InOut); -- The TweenInfo. The part will take 2 seconds to reach either up or down in a perfect situation. It's going to move linearly.
local y = 50; -- Variable for the amount of studs the part will go up.
local cframe; -- Variable for the CFrame that the part will need to reach.

function move_part() -- Declaration of the function that will later be connected with the detector's MouseClick() event.
    if first == false then -- Checks if it's the first click of the user.
        first = true; -- Sets the 'first' variable to true since it is the first click.
        cframe = old_pos * CFrame.new(12, 0, 0) -- Sets the CFrame the part will need to reach. (In this case straight up relative to the Curtain's old position.
    else -- If it's not the first click then it will go here.
        first = false; -- Sets the 'first' variable back to false since it's the second click.
        cframe = old_pos; -- Sets the CFrame back to it's initial position so that it can go back to where it started from.
    end -- end for the if statement to check if it's the first click or second click.

    local goal = {CFrame = cframe}; -- Variable for the goal now that we know what CFrame the part will need to go to.

    local tween = tween_service:Create(curtain, info, goal); -- The whole tween of the Curtain, including the instance, info and goal the part is trying to reach.

    tween:Play(); -- Plays the Tween animation on the part
end -- end for the function.

detector.MouseClick:Connect(move_part); -- Connects the function to the MouseClick() event of the ClickDetector.



1 answer

Log in to vote
0
Answered by 6 years ago
Edited 6 years ago

Hi!

You can do this with a tweenModel function that TigerCaptain made that follows all the other BaseParts's offset to the PrimaryPart, moving the entire model in unity. The model you are trying to move MUST have a PrimaryPart to work.

local Model = workspace.Model --Identify the model you are going to move.
Model.PrimaryPart = Model.Part --Identify the model's PrimaryPart. This is the part that all the other parts will follow. It must be inside the model.

local detector = game.Workspace.Part.ClickDetector;
local tween_service = game:GetService("TweenService")
local curtain = workspace:WaitForChild("Curtain");
local old_pos = Model.PrimaryPart.CFrame;
local first = false;
local info = TweenInfo.new(1, Enum.EasingStyle.Linear, Enum.EasingDirection.InOut);
local y = 50;
local cframe;

local function tweenModel(model,tweenInfo,properties) --The function that moves the entire model.
    if not model.PrimaryPart then return end --If the model doesn't have a PrimaryPart then don't run.
    local details = {}
    local function getParts(par, t)
        local t = t or {}
        for i,v in pairs(par:GetChildren()) do --For every child in the model.
            if v:IsA("BasePart") then --The child is a basepart.
                t[#t+1]=v --Add it to the baseparts list.
            else
                t = getParts(v, t)
            end end
        return t --Return the list of baseparts.
    end
    for i,part in pairs(getParts(model)) do --For every basepart in the model.
        if part ~= model.PrimaryPart then --If the part isn't the primarypart then
            details[part] = model.PrimaryPart.CFrame:toObjectSpace(part.CFrame) --Find the ObjectSpace between the part and the PrimaryPart.
        end end
    local con
    con = model.PrimaryPart.Changed:connect(function()
        for part,offset in pairs(details) do
            part.CFrame = model.PrimaryPart.CFrame*offset --Get the offset between the part and the PrimaryPart so it can follow.
        end end)
    local tween = TweenService:Create(model.PrimaryPart,tweenInfo,properties) --Create the tween.
    tween:Play() --Play the tween.
end

function move_part()
    if first == false then
        first = true;
        cframe = old_pos * CFrame.new(12, 0, 0)
    else
        first = false;
        cframe = old_pos;
    end
local goal = {CFrame = cframe};
    tweenModel(curtain, info, goal); --This will fire the tweenModel with the given Tween variables.
end

detector.MouseClick:Connect(move_part);

Click Accept Answer if this was what you're looking for! Comment if you have any different concerns!

Ad

Answer this question