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

Best way to change the property an object or gui?

Is there a "Best Method" to change the property of an object or GUI, like for example the transparency of a Frame in a GUI. There are two ways that I know of, I could either use a for-loop or TweenService to change the transparency. Is there another way that I am missing?

Does one cause more lag? Can one be more efficient? Does one look more appealing?

Here's a for-loop for the transparency of a block

local block = script.Parent

while true do
	for i = 1, 10 do
		block.Transparency = block.Transparency + 0.1
		wait(1)
	end
	wait(2)
	for i = 1, 10 do
		block.Transparency = block.Transparency - 0.1
		wait(1)
	end
	wait(2)
end

Here's the TweenService for the transparency of a block

local block = script.Parent
local ts = game:GetService("TweenService")
local tween = nil

function tweenObject(t)
	local info = TweenInfo.new(10)
	return ts:Create(block, info, {Transparency = t})
end

while true do
	tween = tweenObject(1)
	tween:Play()
	wait(12)
	tween = tweenObject(0)
	tween:Play()
	wait(12)
end

Which one is the better method to use? Or under some circumstances, which one should be used?

3 comments

0

There is no major performance difference. As you may know, TweenService allows for Enum choice and various other customization options which would otherwise need to be added in via math when it comes to interpolation - which is why it is more preferable in some cases.

0

Note that with a for loop, you can set the step to .1. Thus simplifying your example for loops immensely. Example: for i = 0, 1, .1 do block.Transparency = i. Similarly, you can do a negative step.

1

Example: for i = 1, 0, -.1 do block.Transparency = i.

1 answer

Tweens are basically just for loops. So yeah, both can be used, they have no significant differences in performance. The only thing different is that Tweens do not yield the thread, while for loops yields the thread until it's done. Tweens also have some additional features (easing styles, easing directions, repeat count, etc.). It's up to you to decide which to use in different use cases.

1 comment

0

Tweening is actually more efficient in terms of performance since it's a Roblox thing and not actually Lua

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