How can I do this?

Hi everyone. I’m a little stumped on how I can attempt to make this Frame’s background transparency 0.6 in exactly 1 second. TweenService did work, but tweens can be pretty annoying to deal with sometimes and it was definitely not working for this. How can I make this work?

RunService.Heartbeat:Connect(function(deltaTime)
	UI.BackgroundTransparency += 0.1
end)

Again, my goal is to make the BackgroundTransparency = 0.6 by the time 1 second passes just like TweenService. If you can help me, that’d be greatly appreciated.

local Step = 0
RunService.Heartbeat:Connect(function(deltaTime)
	Step += deltaTime
	UI.BackgroundTransparency = math.max(Step, 1)
end)

(just realised that the math.max is useless because the values get clamped automatically)

So, what should I put in replacement of math.max? So far, it isn’t working. I also changed the += to -= because I realized the += would mean I wanted to add to the transparency instead of removing from it.

What’s wrong with TweenService?
You can do this code in one single line with it,

TweenService:Create(UI, TweenInfo.new(1), {BackgroundTransparency = 0.6}):Play()

unless you have a really big problem with it.

I have a pretty big system, and using TweenService doesn’t work for it. I would use lerping, but I also can’t do that because you can’t lerp transparency sadly.

Made a function for it for reusability

local function Lerp(object, property: string, value: any, seconds: number)
	local startTime = tick()
	local start = object[property]
	local goal = value
	
	-- add task.spawn() to make the function not yield
	while tick() - startTime <= seconds do
		object[property] = start + ((goal - start) * (tick() - startTime) / seconds)		
		task.wait()
	end
end
3 Likes

Thank you so much for this. This worked like a charm. I will forever remember this function for future use. Again, thank you so much. Consider your solution box checked. :smiley:

1 Like

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.