ok I have a rocket that I want to come up out of the ground after two doors open upwards at the same time.
when opening they open up and out
then the rocket will start to raise up out of the ground.
I have been working on this code all day, trying different ways.
none of the ways would work.
path is in the code but here it is anyway
WorkSpace SiloControls SiloRaise(script that opens the doors) Light RaiseMissle SiloDoorOpen(button to open the doors) ClickDetector Bar LeverHandle Silo LeftDoor RightDoor
Ok I have changed this code based on recommendations, but it still seams to be broken.
I have reviewed the new code and I understand the changes all but (part, difference, by)
I am trying to learn new and easier ways to do this so please don't give a - score....
thanks for any help you can offer.
LeftDoor = script.Parent.Silo.LeftDoor function moveY(part, difference, by) --local up = (difference < 0 and -1 or 1) * (by or 0.5) local up if difference < 0 then up = -(by or 0.5) else up = (by or 0.5) end for y = 0, difference, up do part.CFrame = part.CFrame + Vector3.new(0, up, 0) wait() end end function moveZ(part, difference, by) local Left if difference < 0 then Left = -(by or 0.1) else Left = (by or 0.1) end for y = 0, difference, Left do part.CFrame = part.CFrame + Vector3.new(0, Left, 0) wait() end end function spinX(part, angle, by) local spin = (angle < 0 and -1 or 1) * (by or 1) -- degrees for t = 0, angle, spin do part.CFrame = part.CFrame * CFrame.Angles( math.rad( spin ), 0, 0) wait() end end function On() while script.Parent.ActivateButton.On.Value == false do spinX(LeftDoor, 12 - 0, 1) moveY(LeftDoor, 88.5 - 88.3, 0.2) spinX(LeftDoor, 18 - 12, 1) moveY(LeftDoor, 88.7 - 88.5, 0.2) spinX(LeftDoor, 25 - 18, 1) moveZ(LeftDoor, -204.4 - -204.5, .1) moveY(LeftDoor, 88.8 - 88.7, 0.1) spinX(LeftDoor, 28 - 25, 1) moveZ(LeftDoor, -204.35 - -204.4, .05) spinX(LeftDoor, 30 - 28, 1) moveY(LeftDoor, 88.9 - 88.7, 0.1) spinX(LeftDoor, 33 - 30, 1) moveY(LeftDoor, 89 - 88.9, .1) moveZ(LeftDoor, -204.3 - -204.35, .05) spinX(LeftDoor, 37 - 33, 1) moveY(LeftDoor, 89.1 - 89, .1) moveZ(LeftDoor, -204.25 - -204.3, .05) spinX(LeftDoor, 41 - 37, 1) moveY(LeftDoor, 89.2 - 89.1, 0.1) moveZ(LeftDoor, -204.2 - -204.25, .05) spinX(LeftDoor, 45 - 41, 1) moveZ(LeftDoor, -204.1 - -204.2, .05) moveY(LeftDoor, 89.3 - 89.2, 0.1) spinX(LeftDoor, 50 - 45, 1) moveZ(LeftDoor, -204.05 - -204.2, .05) spinX(LeftDoor, 53 - 50, 1) moveY(LeftDoor, 89.4 - 89.3, .1) moveZ(LeftDoor, -204 - -204.2, .05) spinX(LeftDoor, 56 - 53, 1) moveZ(LeftDoor, -203.95 - -204, .05) spinX(LeftDoor, 59 - 56, 1) moveY(LeftDoor, 89.5 - 89.4, .1) moveZ(LeftDoor, -203.9 - -203.95, .05) spinX(LeftDoor, 62 - 59, 1) moveZ(LeftDoor, -203.85 - -203.9, .05) spinX(LeftDoor, 65 - 62, 1) moveZ(LeftDoor, -203.75 - -203.85, .05) spinX(LeftDoor, 67 - 65, 1) moveZ(LeftDoor, -203.7 - -203.75, .05) spinX(LeftDoor, 68 - 67, 1) moveY(LeftDoor, 89.55 - 89.4, .1) moveZ(LeftDoor, -203.65 - -203.7, .05) spinX(LeftDoor, 70 - 68, 1) moveZ(LeftDoor, -203.6 - -203.65, .05) spinX(LeftDoor, 72 - 70, 1) moveZ(LeftDoor, -203.55 - -203.6, .05) spinX(LeftDoor, 74 - 72, 1) moveZ(LeftDoor, -203.5 - -203.55, .05) spinX(LeftDoor, 76 - 74, 1) moveZ(LeftDoor, -203.45 - -203.55, .05) spinX(LeftDoor, 78 - 76, 1) moveZ(LeftDoor, -203.4 - -203.45, .05) spinX(LeftDoor, 79 - 78, 1) moveZ(LeftDoor, -203.35 - -203.4, .05) spinX(LeftDoor, 81 - 79, 1) moveZ(LeftDoor, -203.3 - -203.35, .05) spinX(LeftDoor, 83 - 81, 1) moveZ(LeftDoor, -203.25 - -203.3, .05) spinX(LeftDoor, 86 - 83, 1) moveZ(LeftDoor, -203.2 - -203.25, .05) spinX(LeftDoor, 88 - 86, 1) moveZ(LeftDoor, -203.15 - -203.2, .05) spinX(LeftDoor, 90 - 88, 1) moveZ(LeftDoor, -203.1 - -203.15, .05) end end script.Parent.SiloDoorOpen.LeverHandle.ClickDetector.Click:connect(On)
First:
Your for
loops are nested -- for each tiny step of the the left door at the very beginning, everything else is done also.
You don't want tons of end
s stacked after each other like that, you want each for
loop to end immediately before the next one starts.
You don't have any wait
s in this script, so everything will happen all at once. If you want to see animation, you need there to be pauses.
Second: You have so much repetition. Use a function! Something simple like this:
function moveY(part, fromY, toY) for y = fromY, toY, 0.5 then part.Position = Vector3.new(0, y, 0) end end
However, that's probably not what you want. That will also move it horizontally, since you're saying you want the x and z to both be 0
. Position
is also usually not what you want; you want to set CFrame
.
-- Let's just say how much it moves up, rather than from/to. -- You can just leave in the difference when you call it, e.g., -- moveY(LeftDoor, 88.5 - 88.3) -- in the first time this is used. function moveY(part, difference) local up = 0.5 for y = 0, difference, up do part.CFrame = part.CFrame + Vector3.new(0, up, 0) wait() -- Necessary to *see* this! end end
Another problem is that this can only go up -- if the difference
is negative, the for
loop will just not happen. We need to fix that. Also, we should probably let a different speed for moving up or down be given (optionally):
function moveY(part, difference, by) local up if difference < 0 then up = -(by or 0.5) else up = (by or 0.5) end for y = 0, difference, up do part.CFrame = part.CFrame + Vector3.new(0, up, 0) wait() end end
The definition of up
can be more succinct using the ternary operator:
local up = (difference < 0 and -1 or 1) * (by or 0.5)
Using this moveY
and equivalent moveX
and moveZ
functions, we can get much, much shorter code:
-- this: for i = 88.3, 88.5, .2 do --Moves the position up by .2 each time LeftDoor.Position = Vector3.new(0,i,0) end -- is replaced by this: moveY(LeftDoor, 88.5 - 88.3, 0.2)
We also have to make an equivalent function for rotations:
function spinX(part, angle, by) local spin = (difference < 0 and -1 or 1) * (by or 1) -- degrees for t = 0, angle, spin do part.CFrame = part.CFrame * CFrame.Angles( math.rad( spin ), 0, 0) wait() end end
The resulting code (for the On
function anyway) is much, much clearer and simpler:
function On() while "script.Parent.ActivateButton.On.Value == false" do spinX(LeftDoor, 12 - 0, 1) moveY(LeftDoor, 88.5 - 88.3, 0.2) spinX(LeftDoor, 18 - 12, 1) moveY(LeftDoor, 88.7 - 88.5, 0.2) spinX(LeftDoor, 25 - 18, 1) moveZ(LeftDoor, -204.4 - -204.5, .1) moveY(LeftDoor, 88.8 - 88.7, 0.1) spinX(LeftDoor, 28 - 25, 1) moveZ(LeftDoor, -204.35 - -204.4, .05) spinX(LeftDoor, 30 - 28, 1) moveY(LeftDoor, 88.9 - 88.7, 0.1) spinX(LeftDoor, 33 - 30, 1) moveY(LeftDoor, 89 - 88.9, .1) moveZ(LeftDoor, -204.3 - -204.35, .05) spinX(LeftDoor, 37 - 33, 1) moveY(LeftDoor, 89.1 - 89, .1) moveZ(LeftDoor, -204.25 - -204.3, .05) spinX(LeftDoor, 41 - 37, 1) moveY(LeftDoor, 89.2 - 89.1, 0.1) moveZ(LeftDoor, -204.2 - -204.25, .05) spinX(LeftDoor, 45 - 41, 1) moveZ(LeftDoor, -204.1 - -204.2, .05) moveY(LeftDoor, 89.3 - 89.2, 0.1) spinX(LeftDoor, 50 - 45, 1) moveZ(LeftDoor, -204.05 - -204.2, .05) spinX(LeftDoor, 53 - 50, 1) moveY(LeftDoor, 89.4 - 89.3, .1) moveZ(LeftDoor, -204 - -204.2, .05) spinX(LeftDoor, 56 - 53, 1) moveZ(LeftDoor, -203.95 - -204, .05) spinX(LeftDoor, 59 - 56, 1) moveY(LeftDoor, 89.5 - 89.4, .1) moveZ(LeftDoor, -203.9 - -203.95, .05) spinX(LeftDoor, 62 - 59, 1) moveZ(LeftDoor, -203.85 - -203.9, .05) spinX(LeftDoor, 65 - 62, 1) moveZ(LeftDoor, -203.75 - -203.85, .05) spinX(LeftDoor, 67 - 65, 1) moveZ(LeftDoor, -203.7 - -203.75, .05) spinX(LeftDoor, 68 - 67, 1) moveY(LeftDoor, 89.55 - 89.4, .1) moveZ(LeftDoor, -203.65 - -203.7, .05) spinX(LeftDoor, 70 - 68, 1) moveZ(LeftDoor, -203.6 - -203.65, .05) spinX(LeftDoor, 72 - 70, 1) moveZ(LeftDoor, -203.55 - -203.6, .05) spinX(LeftDoor, 74 - 72, 1) moveZ(LeftDoor, -203.5 - -203.55, .05) spinX(LeftDoor, 76 - 74, 1) moveZ(LeftDoor, -203.45 - -203.55, .05) spinX(LeftDoor, 78 - 76, 1) moveZ(LeftDoor, -203.4 - -203.45, .05) spinX(LeftDoor, 79 - 78, 1) moveZ(LeftDoor, -203.35 - -203.4, .05) spinX(LeftDoor, 81 - 79, 1) moveZ(LeftDoor, -203.3 - -203.35, .05) spinX(LeftDoor, 83 - 81, 1) moveZ(LeftDoor, -203.25 - -203.3, .05) spinX(LeftDoor, 86 - 83, 1) moveZ(LeftDoor, -203.2 - -203.25, .05) spinX(LeftDoor, 88 - 86, 1) moveZ(LeftDoor, -203.15 - -203.2, .05) spinX(LeftDoor, 90 - 88, 1) moveZ(LeftDoor, -203.1 - -203.15, .05) end end
I cannot fathom how you determined these numbers, however.
In addition, the while
loop is wrong. Quotes mean text. Lua ignores that and just says, "okay, you're giving me text, I'm happy about that! Let's loop forever." Remove the quotes for it to make sense.
Don't write huge amounts of code! If it's a struggle, you're probably doing something wrong.
While the code here is immensely better, it could still be significantly improved.