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

Why is my moving door not working?

Asked by 9 years ago

This script is inside a click detector inside a model. But it is not working. It is anchored so parts wont fall out of the model. Im not sure if this should be in a local script or a regular one either.

local Model = Workspace.Model 
local newPos = Model.Position + Vector3.new(0,5,0) 
local Time = 10 
local Increment = 0.5 
local Debounce = false

local Diff = newPos - Model.Position 
local Mag = Diff.magnitude 
local Direction = CFrame.new(Model.Position, newPos).lookVector

function MovePart() 
    if Debounce then return end 
    Debounce = true 
    for n = 0, Mag, Increment do
        Model.CFrame = Model.CFrame + (Direction * Increment)
        wait( (Time/Mag) * Increment )
    end
    Debounce = false 
end

Workspace.Button.ClickDetector.MouseClick:connect(MoveModel)

2 answers

Log in to vote
1
Answered by
adark 5487 Badge of Merit Moderation Voter Community Moderator
9 years ago

Models don't have a CFrame property, only Parts and Unions do. Try setting the Model's PrimaryPart property, and using the SetPrimaryPartCFrame function:

local Model = Workspace.Model 
local newPos = Model.PrimaryPart.Position + Vector3.new(0,5,0) 
local Time = 10 
local Increment = 0.5 
local Debounce = false

local Diff = newPos - Model.PrimaryPart.Position 
local Mag = Diff.magnitude 
local Direction = CFrame.new(Model.PrimaryPart.Position, newPos).lookVector

function MoveModel() 
    if Debounce then return end 
    Debounce = true 
    for n = 0, Mag, Increment do
        Model:SetPrimaryPartCFrame(Model.PrimaryPart.CFrame + (Direction * Increment))
        wait( (Time/Mag) * Increment )
    end
    Debounce = false 
end

Workspace.Button.ClickDetector.MouseClick:connect(MoveModel) --You changed this to `MoveModel` without changing the name of the function. I fixed this for you.
1
Don't you wish that models did though? *sigh* DewnOracle 115 — 9y
1
^ Agreed. It would be SO much easier. groovydino 2 — 9y
0
The problem is that it would be unpredictable in many cases. Be thankful we have SetPrimaryPartCFrame, now. You used to have to move each part using Lua math, which is a lot slower. adark 5487 — 9y
Ad
Log in to vote
0
Answered by
pauljkl 10
9 years ago
local Model = Workspace.Model -- Assuming this is a BasePart
local newPos = Model.Position + Vector3.new(0,5,0)
local Time = 10
local Increment = 0.5
local Debounce = false

local Diff = newPos - Model.Position
local Mag = Diff.magnitude
local Direction = CFrame.new(Model.Position, newPos).lookVector

function MovePart()
    if Debounce then return end
    Debounce = true
    for n = 0, Mag, Increment do
        Model.CFrame = Model.CFrame * CFrame.new(Direction * Increment) --Changed here, CFrame can be a little funny about addition of Vector3
        wait( (Time/Mag) * Increment )
    end
    Debounce = false
end
Workspace.Button.ClickDetector.MouseClick:connect(MoveModel)

0
Pualjkl's idea did not work AruaTheSylveon 0 — 9y
0
Model is a BasePart right? pauljkl 10 — 9y

Answer this question