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

Loop isn't working?

I tried making a script in which the brick slowly disappears and then slowly reappears after, and this goes on infinitely. But it isn't working, here is the script:

t = script.Parent.Transparency

while true do
	for i=1,10 do
		t = t + 0.1
		wait(1)
	end
	wait(1)
	for i=1,10 do
		t = t + 0.1
		wait(1)
	end
end

2 answers

✓ Accepted answer
local t = script.Parent

while true do

    for i=1,10 do
        t.Transparency = t.Transparency + 0.1 -- Addend
        wait(1)
    end

    for i=1,10 do
        t.Transparency = t.Transparency - 0.1 -- Subtraction
        wait(1)
    end
end

2 comments

0

Alright thanks it worked, so do you have to change properties inside of the loop instead of defining them in the variable? Cause I can see that you changed it to where it changes the transparency inside the loop. Either way I'll accept the answer.

0

oh finnaly you understood!!

The script is going disappear forever cause you are ALWAYS adding transparency:

t = script.Parent.Transparency

while true do
    for i=1,10 do
        t = t + 0.1
        wait(1)
    end
    wait(1)
    for i=1,10 do
        t = t - 0.1 -- you said here t = t + 0.1
        wait(1)
    end
end

Protip: Instead of adding, you can make the things happen on i var, like this:

t = script.Parent.Transparency

while true do
    for i = 0, 1, 0.1 do -- it goes from 0 to 1, adding 0.1
        t = i
        wait(1)
    end
    wait(1)
    for i = 1, 0, -0.1 do -- going from 1 to 0, subtracting 0.1
        t = i
        wait(1)
    end
end

as everyone on the comments and I said, you should give objects to the vars and changing their properties, like this:

t = script.Parent

while true do
    for i = 0, 1, 0.1 do -- it goes from 0 to 1, adding 0.1
        t.Transparency = i
        wait(1)
    end
    wait(1)
    for i = 1, 0, -0.1 do -- going from 1 to 0, subtracting 0.1
        t.Transparency = i
        wait(1)
    end
end

8 comments

0

Thanks for making it more efficient, but for some reason it still isn't working.

1

try using t = script.Parent and t.Transparency = i

0

You're script is maybe disabled

0

anything in the output?

0

You can't use properties in variables as the variables only store the value of the property. What dav said above will work if you use t = script.Parent and t.Transparency = i.

0

@Spongecardo, you can set properties as a variable and use them, it only errors when you try to edit the variable

0

now he can understand :)

0

@Lord Yeah, that's what I was trying to say.

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