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.
button = script.Parent

while true do 
    wait(1)
button.Rotation = button.Rotation + 2
if button.Rotation == 20 then
    button.Rotation = button.Rotation - 2
if button.Rotation == - 20 then
    button.Rotation = button.Rotation + 2
    wait(.1)
        end
    end
end

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

button = script.Parent
while true do 
    wait(1)
    button.Rotation = button.Rotation + 2
    if button.Rotation == 20 then
        button.Rotation = button.Rotation - 2
        if button.Rotation == - 20 then
            button.Rotation = button.Rotation + 2
            wait(.1)
        end
    end
end

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.

button = script.Parent
while true do 
    wait(1)
    button.Rotation = button.Rotation + 2
    if button.Rotation == 20 then
        button.Rotation = button.Rotation - 2
    end
    if button.Rotation == - 20 then
        button.Rotation = button.Rotation + 2
    end
end

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.

button = script.Parent
while true do 
    wait(1)
    if button.Rotation == 20 then
        button.Rotation = button.Rotation - 2
    end
    if button.Rotation == - 20 then
        button.Rotation = button.Rotation + 2
    end
end

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:

button = script.Parent
direction = 2
while true do
    wait(1)
    button.Rotation = button.Rotation + direction
    if button.Rotation == 20 then
        direction = -2
        -- Turn around!
    end
    if button.Rotation = -20 then
        direction = 2
        -- Turn around!
    end
end

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)

while true do
    wait(1)
    button.Rotation = button.Rotation + direction
    if button.Rotation >= 20 then
        direction = -2
    end
    if button.Rotation <= -20 then
        direction = 2
    end
end

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:

    ...
    if math.abs(button.Rotation) >= 20 then
        direction = -button.Rotation / 10
    end
    ...



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

button = script.Parent
time = 0
while true do
    time = time + wait(0.2)
    button.Rotation = 20 * math.sin( time )
end
Ad

Answer this question