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

Why won't this scrip work?

Asked by 10 years ago

Please make your question title relevant to your question content. It should be a one-sentence summary in question form.
01button = script.Parent
02 
03while true do
04    wait(1)
05button.Rotation = button.Rotation + 2
06if button.Rotation == 20 then
07    button.Rotation = button.Rotation - 2
08if button.Rotation == - 20 then
09    button.Rotation = button.Rotation + 2
10    wait(.1)
11        end
12    end
13end

What happens is, the Gui rotates to 20 but it won't rotate back down to -20?

1 answer

Log in to vote
16
Answered by
BlueTaslem 18071 Moderation Voter Administrator Community Moderator Super Administrator
10 years ago
Edited 8 years ago

This code is definitely poorly designed.

Let's begin by tabbing your code properly

01button = script.Parent
02while true do
03    wait(1)
04    button.Rotation = button.Rotation + 2
05    if button.Rotation == 20 then
06        button.Rotation = button.Rotation - 2
07        if button.Rotation == - 20 then
08            button.Rotation = button.Rotation + 2
09            wait(.1)
10        end
11    end
12end

Notice that the second if for adding to Rotation is inside the other one. That is almost certainly not what we want.

The wait(.1) similarly doesn't belong, probably.

01button = script.Parent
02while true do
03    wait(1)
04    button.Rotation = button.Rotation + 2
05    if button.Rotation == 20 then
06        button.Rotation = button.Rotation - 2
07    end
08    if button.Rotation == - 20 then
09        button.Rotation = button.Rotation + 2
10    end
11end

We can also see that we're adding twice here instead of just once. That doesn't seem right, and it probably isn't, let's drop it.

01button = script.Parent
02while true do
03    wait(1)
04    if button.Rotation == 20 then
05        button.Rotation = button.Rotation - 2
06    end
07    if button.Rotation == - 20 then
08        button.Rotation = button.Rotation + 2
09    end
10end

Now, here's a problem. Usually, Rotation won't be either 20 or -20. It can be something in between. Let's use a variable for the direction instead, and just change direction when we get to the ends:

01button = script.Parent
02direction = 2
03while true do
04    wait(1)
05    button.Rotation = button.Rotation + direction
06    if button.Rotation == 20 then
07        direction = -2
08        -- Turn around!
09    end
10    if button.Rotation = -20 then
11        direction = 2
12        -- Turn around!
13    end
14end

It would also probably be better to use >= and <= instead of ==, just in case the numbers don't add up to exactly 20 (e.g., if you change the numbers, or things get more complicated)

01while true do
02    wait(1)
03    button.Rotation = button.Rotation + direction
04    if button.Rotation >= 20 then
05        direction = -2
06    end
07    if button.Rotation <= -20 then
08        direction = 2
09    end
10end

We could make this a little cleaner and simpler by using button.Rotation's magnitude (absolute value) instead of the separate numbers 20 and -20:

1...
2if math.abs(button.Rotation) >= 20 then
3    direction = -button.Rotation / 10
4end
5...



Or, instead of the linear jump back and forth, we could make things smooth by using a sin (sine) function:

1button = script.Parent
2time = 0
3while true do
4    time = time + wait(0.2)
5    button.Rotation = 20 * math.sin( time )
6end
Ad

Answer this question