TweenService bug

I would like to know, if we put wait () in front of or behind a transparency tweenservice script it starts only leaving the parts visible or invisible one by one, so if someone knowing the answer could tell me how can I pinpoint this?

1 Like

Can you include a sample script?

ok, wait i gonna take the sample script here

local arm = script.Parent
for i, a in pairs(arm:GetChildren()) do
if a:IsA(“BasePart”) then
while wait(0.5) do
game.TweenService:Create(a,TweenInfo.new(0.5),{Transparency = 1}):Play()
wait(0.5)
game.TweenService:Create(a,TweenInfo.new(0.5),{Transparency = 0}):Play()
end
end
end

Using this layout highlights syntax for code:

`` ` lua

`` ` (remove the spaces)

Also, this is not a bug. It is making the parts change transparency 1 by 1 because you are yielding the loop with waits. Basically, it changes one objects transparency, waits, then it moves onto the next instance. To fix this you can use either the spawn() function or coroutines which allow multiple functions to run at once.

local arm = script.Parent
for i, a in pairs(arm:GetChildren()) do
	spawn(function()
		if a:IsA(“BasePart”) then
			while wait(0.5) do
				game.TweenService:Create(a,TweenInfo.new(0.5),{Transparency = 1}):Play()
				wait(0.5)
				game.TweenService:Create(a,TweenInfo.new(0.5),{Transparency = 0}):Play()
			end
		end
	end)
end

Ew no, please do not use spawn. You don’t need to pseudothread at all for this and spawn is bad. Don’t use while wait either, because that’s also bad. You can easily just run as many for loops as you need for this to work. Play is not a yielding function so you can run your tweens on multiple objects at relatively all the same time.

Actually, in fact, TweenInfo supports use cases like this. There are arguments to determine if the tween should reverse when it finishes, a delay between each tween (including the first one) and how many times it should repeat. The repeating factor includes a way to make it repeat infinitely.

Better method:

local TweenService = game:GetService("TweenService")

-- A long line of arguments looks ugly for me, so I'm line breaking.
local ARM_TWEEN_INFO = TweenInfo.new(
    0.5, Enum.EasingStyle.Quad, Enum.EasingDirection.Out, -1, true, 0.5
)

local arm = script.Parent

for _, armPart in ipairs(arm:GetChildren()) do
    if armPart:IsA("BasePart") then
        TweenService:Create(armPart, ARM_TWEEN_INFO, {Transparency = 1}):Play()
    end
end