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

Why does one repeat loop work and the other doesn't?

Why does this work...

repeat
	script.Parent.Reflectance = script.Parent.Reflectance+0.1
	wait(0.2)
until script.Parent.Reflectance == 1

and this one doesn't?

local a = script.Parent.Reflectance

repeat
	a = a+0.1
	wait(0.2)
until a == 1

Wanted to clear up my curiosity.

6 comments

0

It has to do with adding decimals in Lua. If you add a whole number to a on line 4, and divide by 10, it should fix the problem.

0

Here's a question that was asked awhile ago that relates to your probelm https://scriptinghelpers.org/questions/29524/trouble-with-numeric-for-loop-and-step-variant

1

So instead of "+0.1" you are saying that I should replace it with "+(1/10)"?

0

Yeah. This hasn't been tested, but that would be the first thing I would test.

0

okay, scratch that =3

1

I tested it and it did not work either.

1 answer

✓ Accepted answer

Problem

Declaring the variable "a" in the second loop is saving the object's reflectance as a number - it is not holding a reference to the reflectance property.


When is it appropriate to declare a variable then?

Instead of just creating a variable that holds a number, and changing that number (which evidently will not update the property), you should create one referencing the object you're trying to change - to shorten your code and make it look nicer. Here's an example:

-- You should also make your variable names memorable, instead of just using random characters.
local parent = script.Parent

repeat wait(0.1)
	parent.Reflectance = parent.Reflectance + 0.1
until parent.Reflectance >= 1

This same logic applies with anything else you're trying to change. You want to create a reference to another reference - not it's value if you are to change it.

1 comment

1

Very well answer and explanation. Does this also apply to other loops?

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