Scripting Help (basic)

I am trying to make a parts transparency from going 0 to 1 and back from 1 to 0. I don’t really know how to do functions and I know doing it like this would probably make my game laggier using many duplicates of it for more of the same model:

while true do
game.workspace.(the models name).Transparency = .01
wait(.001)
game.workspace.(the models name).Transparency = .02
wait(.001)
game.workspace.(the models name).Transparency = .03

and so on…

I am trying to make it’s transparency go from 0 to 1 and back from 1 to 0 in 10 seconds overall

That won’t work, just do:

local model = game.Workspace.Model
local ts = game:GetService("TweenService")

local info = TweenInfo.new(
5, Enum.EasingStyle.Sine, Enum.EasingDirection.InOut, 0, false, 0
)

local ending = {Transparency = 1}

local info2 = TweenInfo.new(
5, Enum.EasingStyle.Sine, Enum.EasingDirection.InOut, 0, false, 0
)

local start = {Transparency = 0}

local tween = ts:Create(model, info, ending) -- to play type tween:Play()
local tween2 = ts:Create(model, info2, start) -- and to play this just type tween2:Play()

This is tweening hopefully it’s fine for that.

3 Likes

Adding onto what @GoteeSign said,

TweenService is used to move object/values in Smooth or elastic ways. The thing your trying to accomplish would benefit from this

1 Like

For situations like these you should use TweenService:Create(…), just as the others have mentioned. It also might help in the future to know that even if you call wait() with small values, the shortest time it will yield is approximately 1/30th of a second (0.0333). So even when you call wait(0.001), the time the script will yield will still be approximately 0.0333 seconds. To yield for the smallest amount of time, simply call wait() with no arguments.

1 Like

I’m so sorry. I’m really new to this. Where would I input tween:Play() in the Script? I put it right under the code, and nothing really happening, and to make sure, the script is neverending, right?

Like this:

local model = game.Workspace.Model -- change this directory to your object
local ts = game:GetService("TweenService")

local info = TweenInfo.new(
5, Enum.EasingStyle.Sine, Enum.EasingDirection.InOut, 0, false, 0
)

local ending = {Transparency = 1}
local tween = ts:Create(model, info, ending)
tween:Play() -- play the tween right here

No, you would have to make it artificially loop

local currentTransp = 0 -- or whatever the default transparency is

local function tweenFunc(value) -- function to make it easier
   local model = game.Workspace.Model -- change this directory to your object
   local ts = game:GetService("TweenService")

   local info = TweenInfo.new(
     5, Enum.EasingStyle.Sine, Enum.EasingDirection.InOut, 0, false, 0
   )

   local ending = {Transparency = value}
   local tween = ts:Create(model, info, ending)
   tween:Play()

   tween.Completed:Connect(function() -- detect when the tween ends
      local tweenTo = (currentTransp == 1) and 1 or 0 -- transparency to go to

      coroutine.wrap(function() -- so it doesn't yield
         tweenFunc(tweenTo)
      end)()

      if tweenTo == 1 then
         tweenTo = 0
      else
         tweenTo = 1
      end

       currentTransp = tweenTo
   end)
end

tweenFunc(currentTransp)
-- i haven't tested this so I don't know if it works

Sorry for replying late but here:

local model = game.Workspace.Model
local ts = game:GetService("TweenService")

local info = TweenInfo.new(
	5, Enum.EasingStyle.Sine, Enum.EasingDirection.InOut, 0, false, 0
)

local ending = {Transparency = 1}

local info2 = TweenInfo.new(
	5, Enum.EasingStyle.Sine, Enum.EasingDirection.InOut, 0, false, 0
)

local start = {Transparency = 0}

local tween = ts:Create(model, info, ending) -- to play type tween:Play()
local tween2 = ts:Create(model, info2, start) -- and to play this just type tween2:Play()
while wait(5) do
	tween:Play()
	wait(5)
	tween2:Play()
end
1 Like

still doesn’t seem to work, everything’s anchored, I have two unions in the model