Question

I have a model called door Inside I have a BoolValue named Open I have a model called Top that has all of the door blocks named Work Mabey Comeon and Proboblynot And I have Block that when touched is supposed to make Top move up

Directly inside door I have this script

door = script.Parent
open = door.Open
Top = door.Top

opener = 18
speed = 100
steps = speed

startl = Top.CFrame

function MoveDoorToCFrame(cfrm,dr)
    dr.Work.CFrame = cfrm
dr.Mabey.CFrame = dr.Work.CFrame * CFrame.new(0,-7.2,0)
dr.Comeon.CFrame = dr.Work.CFrame * CFrame.new(0,10.8,0)
dr.Problynot.CFrame = dr.Work.CFrame * CFrame.new(0,10.8,0)
end

function Update()
if speed/steps < 0.5 then
    calc = 1-math.cos(math.rad((-90/speed)*steps*2))
else
    calc = 1+math.sin(math.rad((90/speed)*((speed/2)-steps)*2))
end
MoveDoorToCFrame(startl * CFrame.new(0,(calc/2)*opener,0),Top)
end

Update()
while true do
wait()
if not open.Value and steps < speed then
    steps = steps + 1
    Update()
elseif open.Value and steps > 0 then
    steps = steps - 1
    Update()
end
end 

Inside the button that is supposed to activate on touch I have

script.Parent.Touched:connect(function()
script.Parent.Parent.Open.Value = not script.Parent.Parent.Open.Value
end)

script.Parent.Parent.Open.Changed:connect(Update)
Update()

If you know how to fix this it would be gladly appreciated.

Was it helpful?

Solution 3

Your code indeed needs fixing.

You should NOT use a never-ending loop to make your stuff work (unless that is the only way). You should rather base actions on events.

Consider to use this:

Structure:

Door [Model]
    DoorScript [Script]
    Button [Part]
    DoorOpen [BoolValue]
    Top [Model]
        Mabey [Part]
        Comeon [Part]
        Problynot [Part]

DoorScript:

local Model = script.Parent
local Door = Model.Top
local Button = Model.Button
local DoorOpen = Model.DoorOpen

local Offset = 0
local ToOffset = 100
local Direction = 1

local StepLength = 0.1

local Moving = false

function StartMoving()
    if Moving then return end
    Moving = true
    while (DoorOpen.Value and Offset ~= ToOffset) or (not DoorOpen.Value and Offset ~= 0) do
        local Change = Offset
        Offset = math.max(0,math.min(ToOffset,Offset + StepLength * (DoorOpen.Value and 1 or -1)))
        Change = Offset - Change
        Top:TranslateBy(Vector3.new(0,Change,0))
        wait()
    end
    Moving = false
end
StartMoving()
DoorOpen.Changed:connect(StartMoving)

local Debounce = false
Button.Touched:connect(function()
    if Debounce then return end
    Debounce = true
    DoorOpen.Value = not DoorOpen.Value
    wait(4)
    Debounce = false
end)

You might want to adjust the speed tho.

OTHER TIPS

Update November 2015:

Using PrimaryPart

Since writing this post, ROBLOX has changed a lot in regards to the API. To move a model like requested, you should set the PrimaryPart property of the model to a central part inside the model. This will act as the origin for the model's movements.

You can then use model:SetPrimaryPartCFrame(cframe) to set the CFrame of the model. You can also retrieve this property by using model:GetPrimaryPartCFrame(), although I believe that is just a shortcut method for model.PrimaryPart.CFrame.

In code, it would look like this:

-- Set PrimaryPart:
MODEL.PrimaryPart = MODEL.SomeCentralPart
...

-- CFrame movement:
local movement = CFrame.new(0, 10, 0)

-- Move the model:
MODEL:SetPrimaryPartCFrame(MODEL:GetPrimaryPartCFrame() * movement)

Option A: Use Model's methods

I think you are making this much more difficult than it needs to be. Whenever you run into an issue like this, be sure to check the current APIs provided. The ROBLOX Model object contains a nifty method called 'TranslateBy' which takes a Vector3 argument to translate the model.

Using MODEL:TranslateBy(Vector3) is similar to moving a model via CFrame, since it ignores collisions.

Another alternative is MODEL:MoveTo(Vector3) which moves a whole model to the given Vector3 world position. The downside to this is that it does collide.

One way to get the same MoveTo effect but without collisions can be done with the TranslateBy method:

MODEL:TranslateBy(Vector3Position - MODEL:GetModelCFrame().p)

Option B: Write a custom function to manipulate the model's CFrame

Another alternative would be to manipulate the whole model's CFrame entirely. To do this, you can write a clever function that will move a whole model relative to an 'origin' point. This is similar to moving shapes on a grid given their points and an origin, except in three dimensions. Using ROBLOX's built-in functions, this is much easier though.

A good way to do this would be to write a function that lets you actually assign a CFrame value to a whole model. Another way would be to allow a translation via CFrame too.

Here's an example:

function ModelCFrameAPI(model)

    local parts = {} -- Hold all BasePart objects
    local cf = {} -- API for CFrame manipulation

    do
        -- Recurse to get all parts:
        local function Scan(parent)
            for k,v in pairs(parent:GetChildren()) do
                if (v:IsA("BasePart")) then
                    table.insert(parts, v)
                end
                Scan(v)
            end
        end
        Scan(model)
    end

    -- Set the model's CFrame
        -- NOTE: 'GetModelCFrame()' will return the model's CFrame
        -- based on the given PrimaryPart. If no PrimaryPart is provided
        -- (which by default is true), ROBLOX will try to determine
        -- the center CFrame of the model and return that.
    function cf:SetCFrame(cf)
        local originInverse = model:GetModelCFrame():inverse()
        for _,v in pairs(parts) do
            v.CFrame = (cf * (originInverse * v.CFrame))
        end
    end

    -- Translate the model's CFrame
    function cf:TranslateCFrame(deltaCf)
        local cf = (model:GetModelCFrame() * deltaCf)
        self:SetCFrame(cf)
    end

    return cf
end


-- Usage:
local myModel = game.Workspace.SOME_MODEL
local myModelCF = ModelCFrameAPI(myModel)

-- Move to 10,10,10 and rotate Y-axis by 180 degrees:
myModelCF:SetCFrame(CFrame.new(10, 10, 10) * CFrame.Angles(0, math.pi, 0))

-- Translate by 30,0,-10 and rotate Y-axis by 90 degrees
myModelCF:TranslateCFrame(CFrame.new(30, 0, -10) * CFrame.Angles(0, math.pi/2, 0))

This might be hard.
You might want to look to free models for this one unless the people above get it to work. I, however, do have a script to move a model:

game.Workspace.Model:MoveTo(Vector3.new(0,0,0))

This can be used to move models, try adding something like this into your code. It's more dynamic.

a = Workspace.Model

for i=0.1,40 do
    for i,v in pairs(a:getChildren()) do
        if v:IsA("Part") then
            v.CFrame = CFrame.new(v.CFrame + Vector3.new(0,0.1,0))
        else print("Not a part")
        end
    end
end
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top