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

while statement not following conditions?

Asked by 7 years ago
Edited 7 years ago

I've got a gate that I want to move. Simple enough, but I want the gate to move down 17 studs from its current position.

local gatepos = game.Workspace.Model.gate.gate1.Position.Y - 17

        while game.Workspace.Model.gate.gate1.Position.Y ~= gatepos do
            wait()
            game.Workspace.Model.gate:TranslateBy(Vector3.new(0,-.1,0))
        end

Now the script works fine, only it doesn't stop 17 studs down. It keeps going.

  • I've checked the positions, they're correct.
  • I've tried using other parts, no cigar.
  • I've tried changing it to an entire vector3 value:
local y = game.Workspace.Model.gate.gate1.Position.Y - 17
local x = game.Workspace.Model.gate.gate1.Position.X
local z = game.Workspace.Model.gate.gate1.Position.Z
while game.Workspace.Model.gate.gate1.Position.Y ~= Vector3.new(x,y,z) do
            wait()
            game.Workspace.Model.gate:TranslateBy(Vector3.new(0,-.1,0))
        end

Yet the script doesn't want to work.

Any and all help is appreciated. The script is taking surprisingly longer to make than I'd like.

1 answer

Log in to vote
1
Answered by 7 years ago

I bear good news, your script works great. (though not efficient)

However, it's not stopping because it will never reach 17.

local gatepos = game.Workspace.Model.gate.gate1.Position.Y - 17

while game.Workspace.Model.gate.gate1.Position.Y ~= gatepos do
    print(game.Workspace.Model.gate.gate1.Position.Y)
    wait()
    game.Workspace.Model.gate:TranslateBy(Vector3.new(0,-.1,0))
end

I read your code, and it made perfect sense. So I turned to the famous method of printing to the output for debugging. I'm printing the gate's Y position as it goes down.

Here's a small snippet of it from a very long list:

Output

19.199983596802 19.099983215332 18.999982833862 18.899982452393 18.799982070923 18.699981689453 18.599981307983 18.499980926514 18.399980545044 18.299980163574 18.199979782104 18.099979400635 17.999979019165 17.899978637695 17.799978256226 17.699977874756 17.599977493286 17.499977111816 17.399976730347 17.299976348877 17.199975967407 17.099975585938 16.999975204468 16.899974822998 16.799974441528 16.699974060059

If you can't tell, it never hits 17. Now, you can simply round, but I'd like to help you out.

A more efficient and controlled method of achieving your desired result is to simply do this:

local gate = game.Workspace:WaitForChild("Model"):WaitForChild("gate"):WaitForChild("gate1")

for i = 1,17,0.1 do
    wait()
    gate.Position = Vector3.new(gate.Position.X,gate.Position.Y -0.1,gate.Position.Z)
end

Voila, that achieves what you were hoping for. It will move the gate 17 studs down from its current position. For the 0.1's, you may want to create a variable called speed. But I left it like that so you could understand the script better.

You can use this same block of code to make it go up. You simply change the -0.1 to 0.1.

0
Thanks! Thanks for the advice as well. A 'for' loop is much more efficient. Kareem35 79 — 7y
Ad

Answer this question