For loop, I need help

for transparency = 0,1,0.05 do
			script.Parent.Frame.BackgroundTransparency = transparency
			wait(0.01)
			print("hi")
			if script.Parent.Frame.BackgroundTransparency == hi then
				script.Parent:Destroy()
			end
		end

So, this is basically my script, so, it doesn’t works, like, I want the transparency to change from 1, smoothly to 0, but it’s seems like it doesn’t works, I tried alternatives, I did other numbers but it changed from 0 to 1, so well, I’m new with for loops so help

1 Like

I’d use TweenService to accomplish this:

game:GetService(“TweenService”):Create(frame, TweenInfo.new(1), {BackgroundTransparency =1}):Play()

The for loop isn’t working because your numbers are in the wrong order. For example,

    for transparency = 10, 0, -1 do
       fr.Transparency = transparency/10
       wait(.1)
     end

Would make your transparency go down starting at 10. The division is necessary because GUI transparency is measured on a scale of 0-1.

3 Likes

what is “hi” in your statement here?

I believe that is a good problem to note, transparency can only be set by numbers, not strings.

are you referring to the frame name?

0 ( 30 charactersssssssssssss)

The first thing I’d fix with your code is the formatting; please make sure to use indentation and spaces consistently - it makes your code easier to read for yourself and for others.

for transparency = 0, 1, 0.05 do
    script.Parent.Frame.BackgroundTransparency = transparency
    wait(0.01)
    print("hi")
    if script.Parent.Frame.BackgroundTransparency == hi then
        script.Parent:Destroy()
    end
end

Secondly, script.Parent.Frame.BackgroundTransparency == hi makes no sense in the context of the code you posted - there isn’t a variable called ‘hi’ anywhere in your code you posted. I’ll make an educated guess and assume that what you mean to do is delete script.Parent when you reach the end of the fade.

In that case, you can simply put it after the loop:

for transparency = 0, 1, 0.05 do
    script.Parent.Frame.BackgroundTransparency = transparency
    wait(0.01)
end
script.Parent:Destroy()

Next, if you want to fade starting from transparency 1 to transparency 0, you need to change your for loop to start at 1, finish at 0, and add -0.05 each step:

for transparency = 1, 0, -0.05 do
    script.Parent.Frame.BackgroundTransparency = transparency
    wait(0.01)
end
script.Parent:Destroy()

Finally, since wait() can’t wait for less than 1/30 of a second, you may as well remove the parameter you passed to it:

for transparency = 1, 0, -0.05 do
    script.Parent.Frame.BackgroundTransparency = transparency
    wait()
end
script.Parent:Destroy()

However, I’d recommend using @Intended_Pun’s method using TweenService (if you’re comfortable using it, of course). Hopefully this helps you understand how to fix it :slightly_smiling_face:

4 Likes

For some extra information on this, next time look online for answers before posting a topic on developer forum, this could have easily been answered from this article: Documentation - Roblox Creator Hub

Basically what you were doing was destroying the frame before the loop could do its course.

I encourage you to use TweenService instead, like what Intended_Pun said. This effectively superseded doing transitions yourself and allows you to get the smoothest possible transition. Don’t forget that wait never waits the time you expect; it’s indeterminate.

local TweenService = game:GetService("TweenService")

local tweenInfo = TweenInfo.new(1)
local tweenGoal = {BackgroundTransparency = 1}

local tween = TweenService:Create(script.Parent.Frame, tweenInfo, tweenGoal)
tween:Play()
tween.Completed:Wait()

script.Parent:Destroy()

If you still wanted to use a for loop for this, I still don’t encourage the way you’re incrementing transparency and instead recommend lerping the value of BackgroundTransparency.

1 Like

Yeah, but I got a question, how much like, tweenservice for example, basing on the tweeninfo time, how it changes transparency like, transparency = transparency + 0.01? or how smooth?

The TweenInfo time is simply how long the fade should last, in seconds. You don’t have to deal with adding any values like 0.01.

2 Likes

Here is an article on tween service for you if this is needed: TweenService | Documentation - Roblox Creator Hub

If you need any more help and you can’t find or understand it on the web, make a new topic on it, since this new discussion is not about this topic.

I think he’s asking how tweenservice does a smooth fade.

1 Like

I think that was four years ago.

3 Likes

yeah i just realized that later when i posted the reply lol.
Sorry.

1 Like