Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
COMMUNITY QUESTION

Why is my moving door not working?

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

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.

3 comments

1

Don't you wish that models did though? sigh

1

^ Agreed. It would be SO much easier.

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.

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)

2 comments

0

Pualjkl's idea did not work

0

Model is a BasePart right?

This discussion is preserved as it was. New answers, comments, and votes are closed.