Stop a "for = i" loop at the exact number

local gui = game.StarterGui.SodaDiscoveredGUI.Frame.SodaName

ProximityPrompt.Triggered:connect(function(Player)
	for i = 0, 1, 0.01 do
		gui.TextTransparency -= i
		gui.TextStrokeTransparency -= i
		task.wait(0.1)
		
		if gui.TextStrokeTransparency == 0 and gui.TextTransparency == 0 then
			break
		end
	end	
end)

Short and simple. I need this to make it so the GUI.TextTransparency and GUI.TextStrokeTransparency stop at exactly 0. They just keep going until like 50.

If you want to make it end at 0 you can just do


local gui = game.StarterGui.SodaDiscoveredGUI.Frame.SodaName

ProximityPrompt.Triggered:connect(function(Player)
	for i = 0, 1, 0.01 do
       if i == 0 then
           break
               end
		gui.TextTransparency -= i
		gui.TextStrokeTransparency -= i
		task.wait(0.1)
		
		if gui.TextStrokeTransparency == 0 and gui.TextTransparency == 0 then
			break
		end
	end	
end)

1 Like

This is because you’re subtracting i every iteration. On iteration 1, gui.TextTransparency is reduced by 0.01 which equals 0.99. On iteration 2, gui.TextTransparency is reduced by 0.02, which equals 0.97, and so on. It isn’t decreasing at a linear rate.

To fix that, replace -= i with = 1 - i.

gui.TextTransparency = 1 - i
gui.TextStrokeTransparency = 1 - i

Also, if you’re looking to smoothly transition UI, I suggest looking into TweenService.

1 Like

Just use tween service, it would be faster than writing external lua code.

2 Likes

Would you mind giving a light explanation about TweenService? I’ve look into it before, but couldn’t figure it out.

if i == number then break end

thank you for coming to my ted talk

local ts = game:GetService(“TweenService”)
local one = ts:Create(gui,TweenInfo.new(2,Enum.EasingStyle.Cubic,Enum.EasingDirection.Out,0,false,0),{TextStrokeTransparency = 0, TextTransparency = 0)
one:Play()

1 Like

So, this actually worked, but oddly enough, the text doesn’t appear. It says the transparency is 0, but it simply isn’t there.


local ProximityPrompt = script.Parent
local gui = game.StarterGui.SodaDiscoveredGUI.Frame.SodaName
local ts = game:GetService("TweenService")
local one = ts:Create(gui,TweenInfo.new(2,Enum.EasingStyle.Cubic,Enum.EasingDirection.Out,0,false,0),{TextTransparency = 0})

ProximityPrompt.Triggered:connect(function(Player)
	
	print("Soda Collected")
	one:Play()
	
end)

I know this sounds like a stupid question, but are you sure it’s actually there before the tween is played?

Yup.

Oh, you’re changing the StarterGui, not the PlayerGui

Should I use the PlayerGUI instead? And if so, how?

-- Services
local ps = game:GetService("Players")
local ts = game:GetService("TweenService")

-- Local Variables
local pp = script.Parent

-- Script
pp.Triggered:connect(function(Player)
        local pgui = Player:WaitForChild("PlayerGui")
        local gui = pgui:WaitForChild("SodaDiscoveredGUI")
        local guiFrame1 = gui:WaitForChild("Frame")
        local guiSodaName = gui:WaitForChild("SodaName")
        local one = ts:Create(guiSodaName,TweenInfo.new(2,Enum.EasingStyle.Cubic,Enum.EasingDirection.Out,0,false,0),{TextTransparency = 0})
	    one:Play()
        repeat wait() until one.PlaybackState == Enum.PlaybackState.Completed
        script.Parent:Destroy()
end)

In my opinion, using TweenService for something like this is a bit pointless; now you are achieving pretty much the same desired result but with more created instances and complexity.

What other solution would you recommend?

I’m simply trying to find a way to make it so when you interact with a ProximityPrompt, it slowly adds text at the bottom of the screen for a short time, then the text fades away. This is being done in such a way where the script will be in every item. (There’s a lot of them).

You are overcomplicating your code. The break statement is unnecessary, and as with most times you use break to exit a loop, there is probably a better way.

This is how I’d do it:

local gui = game.StarterGui.SodaDiscoveredGUI.Frame.SodaName

ProximityPrompt.Triggered:connect(function(Player)
	for i = 0, 1, 0.01 do
		gui.TextTransparency = 1 - i
		gui.TextStrokeTransparency = 1 - i
		task.wait(0.1)
	end	
end)

The for loop will end when it reaches 1.

I think you meant = not -=

Yes I did, you’re right. I was thinking of -= because that is what you were kind of doing before, but that’s not right for my use case.

Just use a for loop, but listen to the most recent people on how to do it correctly.

This makes the transparency go to 0.01, which is good. It seems that it doesn’t make the text appear. I had this issue in this topic earlier.

I checked that the text was there and everything.