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

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

Asked by
RAYAN1565 691 Moderation Voter
8 years ago

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.

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. User#11440 120 — 8y
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 User#11440 120 — 8y
1
So instead of "+0.1" you are saying that I should replace it with "+(1/10)"? RAYAN1565 691 — 8y
0
Yeah. This hasn't been tested, but that would be the first thing I would test. User#11440 120 — 8y
View all comments (2 more)
0
okay, scratch that =3 User#11440 120 — 8y
1
I tested it and it did not work either. RAYAN1565 691 — 8y

1 answer

Log in to vote
1
Answered by 8 years ago

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
Very well answer and explanation. Does this also apply to other loops? RAYAN1565 691 — 8y
Ad

Answer this question