I currently need help on fixing this. The tween loop doesn’t work when it’s inside a loop.
Code :
local function distort()
for i, distortion in pairs(workspace:GetDescendants()) do
if distortion:IsA("BasePart") then
if distortion.Name == "Distortion" then
parts = distortion
end
end
end
local tween = ts:Create(parts, TweenInfo.new(2.5, Enum.EasingStyle.Sine, Enum.EasingDirection.InOut, -1, true), {Transparency = .99})
tween.Completed:Wait()
tween:Play()
end
game:GetService("RunService").RenderStepped:Connect(distort)
You wait for it to be completed before playing, change the order around.
Though you may also need a debounce because this will make the tween happen once every render stepped.
Judging by the variable being called parts, are you trying to tween multiple parts? If so, you may need to store all distortions in a table and then loop through and make and play their tweens
A debounce is basically a boolean that prevents code from running often, only when it should run. In your case it’s needed cause the tweens would play constantly
Because RenderStepped creates a new thread of execution every frame (by virtue of it firing an event every frame) any yielding code will have no effect.
Tween.Completed:Wait will only yield the current thread of execution. It should also be noted that these statements are in the wrong order, the tween should be played before the thread is yielded until the tween’s completion. A ‘debounce’ variable can be used to indicate if or not the tween should be played.
local debounce --Debounce variable.
local function distort()
if debounce then return end --If the debounce is enabled ignore the render step event.
for i, distortion in pairs(workspace:GetDescendants()) do
if distortion:IsA("BasePart") then
if distortion.Name == "Distortion" then
parts = distortion
end
end
end
debounce = true --Toggle the debounce variable on.
local tween = ts:Create(parts, TweenInfo.new(2.5, Enum.EasingStyle.Sine, Enum.EasingDirection.InOut, -1, true), {Transparency = .99})
tween:Play() --Play the tween.
tween.Completed:Wait() --Wait for the tween's completion.
debounce = false --Toggle the debounce variable off.
end
game:GetService("RunService").RenderStepped:Connect(distort)