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

What's wrong with my part-moving loop?

Asked by 10 years ago

I want my part(which is the parent) to move up and down infinitely. Why isn't it moving? Am I supposed to use CFrame or ..?

Pos = script.Parent.Position.Y

while wait(.1) do
    Pos = Pos + .05
    wait(.1)
    Pos = Pos + .1
    wait(.1)
    Pos = Pos + .1
    wait(.1)
    Pos = Pos + .1
    wait(.1)
    Pos = Pos + .1
    wait(.1)
    Pos = Pos + .1
    wait(.1)
    Pos = Pos + .1
    wait(.1)
    Pos = Pos + .1
    wait(.1)
    Pos = Pos + .1
    wait(.1)
    Pos = Pos + .05
    wait(.1) -------------------------------------------------
    Pos = Pos - .05
    wait(.1)
    Pos = Pos - .1
    wait(.1)
    Pos = Pos - .1
    wait(.1)
    Pos = Pos - .1
    wait(.1)
    Pos = Pos - .1
    wait(.1)
    Pos = Pos - .1
    wait(.1)
    Pos = Pos - .1
    wait(.1)
    Pos = Pos - .1
    wait(.1)
    Pos = Pos - .1
    wait(.1)
    Pos = Pos - .05
end

2 answers

Log in to vote
2
Answered by
DataStore 530 Moderation Voter
10 years ago

1) You cannot set X, Y and Z independently in the way that you have.
2) You shouldn't set a variable to an instance's property directly, since the variable will become what the property's current value is rather than pointing to it. 3) You could shrink the size of your code by using for loops.

So, if you wanted a part to move up and down by 0.9 studs you could do:

while wait(0.1) do
    for i = 1, 9 do
        wait(0.1)
        script.Parent.Position = script.Parent.Position + Vector3.new(0, 0.1, 0)
    end
    for i = 1,9 do
        wait(0.1)
        script.Parent.Position = script.Parent.Position - Vector3.new(0, 0.1, 0)
    end
end
Ad
Log in to vote
1
Answered by
BlueTaslem 18071 Moderation Voter Administrator Community Moderator Super Administrator
10 years ago

Your assignment in line 1,

Pos = script.Parent.Position.Y

does not somehow link your value of Pos to the position you are referring to. script.Parent.Position.Y is just a number so your Pos is just a name for a number.

Modifying Pos in no way modifies script.Parent.Position.Y.

You need to explicitly set the position somewhere.



I would highly recommend using loops to do those repetitive motions, rather than copying and pasting the line over and over.

while wait(.1) do
    -- Up
    local startPos = script.Parent.Position;
    for y = 0, 1, .1 do
        script.Parent.Position = startPos + Vector3.new(0, y, 0);
        wait(.1);
    end
    -- Down
    for y = 1, 0, -.1 do
        script.Parent.Position = startPos + Vector3.new(0, y, 0);
        wait(.1);
    end
end


EDIT:

It would be better to use CFrame because of some quirks with objects interacting when you just set position, though both will work most of the time.

It could look like this:

while wait(.1) do
    -- Up
    local startCFrame = script.Parent.CFrame;
    for y = 0, 1, .1 do
        script.Parent.CFrame= startCFrame + Vector3.new(0, y, 0);
        wait(.1);
    end
    -- Down
    for y = 1, 0, -.1 do
        script.Parent.CFrame = startCFrame + Vector3.new(0, y, 0);
        wait(.1);
    end
end

Answer this question