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

Why does this For loop break and then run over again?

I am using a for loop to make a textlabel flash its transparency. it seems to break because it prints fno but then it repeats again for 30 loops.

--here is the code to make the icon flash on peoples screens
	
	if promptobject.Name == "A" then
		local A = objgui.Holder.PointA
		local Awarner = objgui.Holder.PointAWarner
		
		for L = 1, 30, 1 do
			if cappingA == true then
				for i = 1, 10, 1 do
					Awarner.BackgroundTransparency = Awarner.BackgroundTransparency - 0.05
					wait(0.05)
						print'1'
				end

				for i = 1, 10, 1 do
					Awarner.BackgroundTransparency = Awarner.BackgroundTransparency + 0.05
					wait(0.05)
						print'2'
				end
			elseif cappingA == false then
				break
			end
			
			wait(1)
		end
		print 'fno'
		Awarner.BackgroundTransparency = 1

1 comment

0

What do you mean? Do you mean the 'fno' printing occurs 30 times?

1 answer

✓ Accepted answer

When trying to fade the properties of an instance, it's recommended to use TweenService because it'll make your code look cleaner and shorter.

When making a Tween , you need a Constructor and a dictionary of the properties you're trying to tween.

local instance = workspace.Part 

local tweenInfo = TweenInfo.new(
    5, -- Time it takes for the tween to finish
    Enum.EasingStyle.Linear, -- EasingStyle
    Enum.EasingDirection.In, -- EasingDirection
    -1, -- This is the number of times it'll repeat. Setting this to a negative integer, it'll repeat the tween indefinitely
    true, -- This is whether it'll repeat or not
    0 -- this is a delay between each tween before it starts
)

local instanceProperties = {
    Transparency = 1
    }

--[[
So, in the dictionary, there is a key and value. The key is the property name. The value is the goal of the tween.
When setting reverse on the tween, it will reach its goal and go back to the way it was.
]]

local tween = game:GetService("TweenService"):Create(instance, tweenInfo, instanceProperties)
tween:Play()

--[[
So, that seemed like a lot of code. I'd prefer the default properties of tweenInfo except for the duration of the tween so it will look like this.
]]


game:GetService("TweenService"):Create(workspace.Part, TweenInfo.new(5), {Transparency = 1})

--[[
As you see that was 1 line of code if you viewed the TweenInfo wiki page I gave you and look at the constructor, you'll see all the values have a default value.
]]

Any questions? Just reply to my answer.

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