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

How to use for loops properly?

Asked by 9 years ago
Door = game.Workspace.Door
Button = game.Workspace.Button

local buttonPressed = false

Workspace.Button.Touched:connect(function(hit)
    if not buttonPressed then
        buttonPressed = true
            for i=0,5,.1 do
            Door.CFrame = CFrame.new(86.48, 4.89, -i)
                wait(3)
            Door.CFrame = CFrame.new(86.48, 4.89, i)
        buttonPressed = false
        end
    end
end)



My first problem is the part (Door) is moved on the Z axis to what I guess is either 0 or .1 while I just want to stay in it's current place (86.48, 4.89, -35.935) and just move to the left 5 studs, which is working. I also don't understand how to make it move back into place after 3 seconds but isn't working and I don't have any idea how to fix this one.

2 answers

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

SlickPwner is wrong about a numeric for loop only using two numbers. The third, step, is usable, your code is just messed up in other ways.

First of all, your debounce needs to open again after the loop, not inside it.

What it looks like to me is you want to slide the door open, wait three seconds, then slide it shut. This requires two loops, which each need a wait() inside them.

Lastly, you have to add the Door's initial Z-position to the CFrame when moving it, or else it will be set to i, which is why it was teleporting to near the origin.

Door = game.Workspace.Door
Button = game.Workspace.Button

local startZ = Door.Position.Z

local buttonPressed = false

Workspace.Button.Touched:connect(function(hit)
    if not buttonPressed then
        buttonPressed = true
        for i = 5, 0, -.1 do
            Door.CFrame = CFrame.new(86.48, 4.89, startZ + i)
            wait(.1)
        end
        wait(3)
        for i = 0, 5,  .1 do
            Door.CFrame = CFrame.new(86.48, 4.89, startZ + i)
            wait(.1)
        end
        buttonPressed = false --This needs to be outside the loop, or else 
    end
end)

Side note: why does everybody use Spaces instead of Tabs for indentation? It's so annoying!

Ad
Log in to vote
-1
Answered by 9 years ago

You have a few problems here. First of all, you have 3 numbers separated by commas in the for line, when you should only have 2.

Secondly, you have messed up the code inside the for loop. I'll rewrite your code, and explain it in comments.

Door = game.Workspace.Door
Button = game.Workspace.Button

local buttonPressed = false

Button.Touched:connect(function(hit) --You don't need Workspace if you already declared the variable.
    if buttonPressed == false then --I prefer "== false" to "not," just personal preference.
        buttonPressed = true
            for i=1,10 do 
            Door.CFrame = CFrame.new(86.48, 4.89, i) 
                wait(.1)
            end
    end
end)

This should work. Hope I helped!

0
This is was really helpful but I think there needs to be a wait somewhere because when I tried to use this my studio crashed. markel60 5 — 9y
0
Hmm, I added a wait() in there, see if that helps. SlickPwner 534 — 9y
0
No more crashing but the script doesnt do anything. markel60 5 — 9y
0
Try now, I simplified it a lot. It just moves it 10 block, rather than to a specific position. I think I messed up the old for line anyways. SlickPwner 534 — 9y
View all comments (2 more)
0
This works but I still want it to move from it's original position. This moves it to the middle of the map and then moves 10 studs markel60 5 — 9y
0
Just change the "1" in "i=1" to whatever the starting point of your door is. Alternatively, you can make it "i=Door.Position.z", as I believe that works. SlickPwner 534 — 9y

Answer this question