Want tween service to turn invisible on completion

Hello, I have been trying to make a script in which once the tween service has been completed, the brick will turn invisible until the tween service is played again. However, once the tween is completed, it doesn’t turn invisible.

local TweenService = game:GetService("TweenService")
local part = script.Parent
local targetBrick = game.Workspace.TargetBrick

local tweenInfo = TweenInfo.new(
	15,
	Enum.EasingStyle.Linear,
	Enum.EasingDirection.Out,
	-1,
	false,
	5
)

local tween = TweenService:Create(part,tweenInfo,{Position = targetBrick.Position})

tween.Completed:Connect(function(playbackState)
	if playbackState == Enum.PlaybackState.Completed then
		print("COmpleted!")
	 part.Transparency = 1 
	end
end)

tween:Play()
while true do 
wait()
if(part.Position == targetBrick.Position) then
	part.Transparency = 1
else
	part.Transparency = 0
	end
end

Your while true do loop may be causing this. I would recommend removing the loop and letting the Tween.Completed signal do its thing!

That didn’t work, I tried that before.

Remove the While loop.

In your Completed function, does the “COmpleted!” text print?

It doesnt print, and I think that could be related to the problem.

have you ever tried just putting tween.Completed:Wait() then making the part transparency set to 1 after?

I tested the code in my own studio and found that the issue was within your TweenInfo.
If you would like to add delay time, then you should consider task.wait(number_of_seconds).

Fixed code:

local TweenService = game:GetService("TweenService");

local part = script.Parent
local targetBrick = game.Workspace.TargetBrick

local tweenInfo = TweenInfo.new(
	15,
	Enum.EasingStyle.Linear,
	Enum.EasingDirection.Out
);

local tween = TweenService:Create(part,tweenInfo,{Position = targetBrick.Position})

tween.Completed:Connect(function(playbackState)
	if playbackState == Enum.PlaybackState.Completed then
		print("COmpleted!")
		part.Transparency = 1 
	end
end)

task.wait(5); -- this is your delay time
tween:Play()

The delay value in TweenInfo should work without any issue. :slight_smile:

The script does make it transparent, but it doesn’t loop afterwards which is a problem.

It certainly does! :smiley:
TweenBase.Completed just wasn’t firing due to other forms of data that the OP included.

Please be more specific on what you would like to achieve

The tween.Completed wasn’t firing because the brick didn’t change position :smiley:

Edit: my fault, I put the script inside TargetBrick itself, because I thought Part & targetBrick was the same thing.

1 Like

Basically, I want the tween to fire first, and once completed the brick goes to transparent and waits a certain amount of time before becoming visible again and the loop playing again.

What is that certain amount of time?

I’d say atleast 5 seconds or so.

You can achieve this by saving state values of your original properties!

Desired code:

local TweenService = game:GetService("TweenService");

local part = script.Parent
local targetBrick = game.Workspace.TargetBrick

local originalPos = part.Position
local tweenInfo = TweenInfo.new(
	15,
	Enum.EasingStyle.Linear,
	Enum.EasingDirection.Out
);

local tween = TweenService:Create(part,tweenInfo,{Position = targetBrick.Position});

while task.wait(5) do
    part.Position = originalPos
    part.Transparency
    tween:Play();

    tween.Completed:Wait();
    part.Transparency = 1
end
1 Like
local TweenService = game:GetService("TweenService");
local part = script.Parent
local targetBrick = game.Workspace.TargetBrick
local tweenInfo = TweenInfo.new(1,Enum.EasingStyle.Linear,Enum.EasingDirection.Out,0,false,5);
while true do
local tween = TweenService:Create(part,tweenInfo,{Position = targetBrick.Position})
tween:Play()
tween.Completed:Wait()
part.Transparency = 1
task.wait(5)
part.Transparency = 0
end