I'm looking to make a bunker door that goes up, then down. Yes I have made the measurements, but when I tested it, it didn't even move when I clicked the button? Here's a sample:
1 | while game.Workspace.NAAFBunker.Door.Position.y ~ = 20.1 do |
2 | game.Workspace.NAAFBunker.Door.Position.y = game.Workspace.NAAFBunker.Door.Position.y + 0.1 |
3 | wait( 0.1 ) |
4 | end |
5 | wait( 5 ) |
6 | while game.Workspace.NAAFBunker.Door.Position.y ~ = 11.1 do |
7 | game.Workspace.NAAFBunker.Door.Position.y = game.Workspace.NAAFBunker.Door.Position.y - 0.1 |
8 | wait( 0.1 ) |
9 | end |
You need to use CFrame, not Position, when moving the door.
ROBLOX is stupid, and if you use Position, it won't allow intersections between two difference parts.
As other people have pointed out, you also apparently can't set on a single axis.
01 | door = Workspace.NAAFBunker.Door |
02 |
03 | -- Don't use ~= and == when working with decimals |
04 | while door.Position.y < 20.1 do |
05 | door.CFrame = door.CFrame * CFrame.new( 0 , 0.1 , 0 ) |
06 | wait( 0.1 ) |
07 | end |
08 |
09 | wait( 5 ) |
10 |
11 | while door.Position.y > 11.0 do |
12 | door.CFrame = door.CFrame * CFrame.new( 0 , 0.1 , 0 ) |
13 | wait( 0.1 ) |
14 | end |
However, you can also use the strangely named "lerp" method for slightly nicer, more efficient code.
01 | door = Workspace.NAAFBunker.Door |
02 | height = door.Size.y |
03 | closedCFrame = door.CFrame |
04 | openCFrame = closedCFrame * CFrame.new( 0 , height, 0 ) |
05 |
06 | for i = 0 , 1 , 0.05 do |
07 | door.CFrame = closedCFrame:lerp(openCFrame, i) |
08 | wait( 0.1 ) |
09 | end |
10 |
11 | wait( 5 ) |
12 |
13 | for i = 0 , 1 , 0.05 do |
14 | door.CFrame = openCFrame:lerp(closedCFrame, i) |
15 | wait( 0.1 ) |
16 | end |
or something along those lines
The reason we don't use == and ~= when working with decimals is because computers don't actually store them properly. What looks like a 2.1 is probably actually a 2.1000000003 or something.
The operators == and ~= check for exact equality, so if we're checking for 2.1 and the real value is 2.100000003, they won't work as we expect.
When attempting to move a part, you must use Vector3 or CFrame. You cannot operate on a position regularly
01 | x = game.Workspace.NAAFBunker.Door.Position.X |
02 | y = game.Workspace.NAAFBunker.Door.Position.Y |
03 | z = game.Workspace.NAAFBunker.Door.Position.Z |
04 |
05 | while y < = 20.1 do |
06 | game.Workspace.NAAFBunker.Door.Position = Vector 3. new(x,y+ 0.1 ,z) |
07 | wait( 0.1 ) |
08 | end |
09 |
10 | wait( 5 ) |
11 |
12 | while y > = 11.1 do |
13 | game.Workspace.NAAFBunker.Door.Position = Vector 3. new(x,y- 0.1 ,z) |
14 | wait( 0.1 ) |
15 | end |