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

What is wrong with this loop?

Asked by 9 years ago
local gon = game.Workspace.lol
local Tame = 8
wait (Tame)
while (Tame >.1) do
wait (Tame)
if Tame - 2 == 6 or 2
then gon.BrickColor = BrickColor.new("Really red")
else
gon.BrickColor = BrickColor.new("Black")
end 
Tame = Tame - 2
end


This was me testing loops by trying to use them to make a brick change it's color repeatedly. This changes once and then stops changing. Why is that?

1 answer

Log in to vote
1
Answered by
BlueTaslem 18071 Moderation Voter Administrator Community Moderator Super Administrator
9 years ago

Tame - 2 == 6 or 2 doesn't act the way you think it does.

The order of operations says it should be grouped like this:

if (Tame - 2 == 6) or 2 then

Which is not what you meant. Since 2 is a value, this will actually always happen, making what you wrote equivalent to

if true then

This means it's always the first color.



If you want to check two values, you have to do the full check explicitly (there isn't a shortcut, and can't be for a few reasons):

if Tame - 2 == 6 or Tame - 2 == 2 then

Though you could rearrange that to be perhaps a little more clearer:

if Tame == 8 or Tame == 4 then


As a style note, your spacing for then is very non-standard.

Usually then is on the same line as the corresponding if, and is immediately followed by a break:

if Tame - 2 == 6 or Tame - 2 == 2 then
    gon.BrickColor = BrickColor.new("Really red")

Also remember that you should tab your code, grouping things inside loops or conditions by putting them at the same horizontal level inwards.

Ad

Answer this question