im trying to make a elevator but when the script changes the position the brick rotates any fix?
heres my code...
ewx,ewy,ewz = 1.5,6.5,34 ewx2,ewy2,ewz2 = 7,6.5,28.5 ewx3,ewy3,ewz3 = 0.5,6.5,25 erx,ery,erz = 1.5,11.5,29.5 egx,egy,egz = 1.5,1.5,29.5 time = 0.1 ev1 = workspace.elevatorwall ev2 = workspace.elevatorwall2 ev3 = workspace.elevatorwall3 er = workspace.elevatorroof eg = workspace.elevatorground function elevator() repeat ev1.CFrame = CFrame.new(ewx,ewy,ewz) ewy = ewy + 1 ev2.CFrame = CFrame.new(ewx2,ewy2,ewz2) ewy2 = ewy2 + 1 ev3.CFrame = CFrame.new(ewx3,ewy3,ewz3) ewy3 = ewy3 + 1 er.CFrame = CFrame.new(erx,ery,erz) ery = ery + 1 eg.CFrame = CFrame.new(egx,egy,egz) egy = egy + 1 wait(time) until workspace.elevatorground.CFrame == CFrame.new(1.5,60.5,29.5) end script.Parent.MouseButton1Click:connect(elevator)
CFrame
is used to define the Position
and orientation of any Part within a 3D space, meaning your elevator would only move from a certain position based on whatever CFrames you defined for your elevator parts. Because no Rotation
is defined for any of the parts, whatever rotation your Parts are set inside your place won't be preserved when you move the elevator.
A good solution is to instead move the elevator using Vector3s:
ev1.CFrame = ev1.CFrame + Vector3.new(0, 1, 0)
The line of code above means that ev1
will move up by a Increment of 1 from it's current position. This can be used in a for
loop to move the elevator up by 1 at a time.
for i = 0, 10 do ev1.CFrame = ev1.CFrame + Vector3.new(0,1,0) wait(1) -- wait(1) is used to prevent the loop from running instantly and lets the elevator move up slowly end
Alright, complete solution here:
local par = script.Parent.Parent.Parent -- I grouped all the elevator parts into a Model (par), which is the better solution rather than calling workspace every time. -- All the ewx stuff is unnecessary for this ev1 = par.elevatorwall ev2 = par.elevatorwall2 ev3 = par.elevatorwall3 er = par.elevatorroof eg = par.elevatorground function elevator() -- FOR loop that will move the elevator up by 1 for a total of 10 seconds for i = 0, 10 do ev1.CFrame = ev1.CFrame + Vector3.new(0,1,0) ev2.CFrame = ev2.CFrame + Vector3.new(0,1,0) ev3.CFrame = ev3.CFrame + Vector3.new(0,1,0) er.CFrame = er.CFrame + Vector3.new(0,1,0) eg.CFrame = eg.CFrame + Vector3.new(0,1,0) wait(0.5) -- Again, wait(0.5) is used to prevent the for loop from running instantly -- You also don't need to define a time variable for wait(), just put in a number end end script.Parent.MouseClick:Connect(elevator) -- Not sure how MouseButton1Click would work, because it's used for a GUI button. Instead, this script is inside a ClickDetector that is inside a Part that will be the button -- Another note, it's preferable to use :Connect instead of :connect, because :connect is deprecated (meaning it's not recommended for you to use, but you can still use it regardless)
Hope this helps!