Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
COMMUNITY QUESTION

What is wrong with this loop?

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

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.

This discussion is preserved as it was. New answers, comments, and votes are closed.