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 ..?

01Pos = script.Parent.Position.Y
02 
03while wait(.1) do
04    Pos = Pos + .05
05    wait(.1)
06    Pos = Pos + .1
07    wait(.1)
08    Pos = Pos + .1
09    wait(.1)
10    Pos = Pos + .1
11    wait(.1)
12    Pos = Pos + .1
13    wait(.1)
14    Pos = Pos + .1
15    wait(.1)
View all 43 lines...

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:

01while wait(0.1) do
02    for i = 1, 9 do
03        wait(0.1)
04        script.Parent.Position = script.Parent.Position + Vector3.new(0, 0.1, 0)
05    end
06    for i = 1,9 do
07        wait(0.1)
08        script.Parent.Position = script.Parent.Position - Vector3.new(0, 0.1, 0)
09    end
10end
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,

1Pos = 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.

01while wait(.1) do
02    -- Up
03    local startPos = script.Parent.Position;
04    for y = 0, 1, .1 do
05        script.Parent.Position = startPos + Vector3.new(0, y, 0);
06        wait(.1);
07    end
08    -- Down
09    for y = 1, 0, -.1 do
10        script.Parent.Position = startPos + Vector3.new(0, y, 0);
11        wait(.1);
12    end
13end


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:

01while wait(.1) do
02    -- Up
03    local startCFrame = script.Parent.CFrame;
04    for y = 0, 1, .1 do
05        script.Parent.CFrame= startCFrame + Vector3.new(0, y, 0);
06        wait(.1);
07    end
08    -- Down
09    for y = 1, 0, -.1 do
10        script.Parent.CFrame = startCFrame + Vector3.new(0, y, 0);
11        wait(.1);
12    end
13end

Answer this question