Tweening Multiple Groups that Delete Themselves

I have scripted a cloud spawner that clones clouds from a group of 4 cloud models and positions them in random spots on one side of the map and then activates a script inside the cloned cloud that tweens it to the other side of the map and then deletes itself.

Is there a more efficient way to do this? Specifically I am wondering if there is a way to tween multiple models that delete themselves upon completion of the tween without having a separate script in each model that tweens and deletes them.

My first thought was to tween them after cloning and positioning them in the same repeating function but I do not know how to delete them from there. The function that clones, positions, and tweens the clouds has to repeat every 5 seconds and having another function inside it that waits for the tween to complete and then destroys the Cloud gets in the way of this.

use task.spawn for this. here’s an example script:

task.spawn(function()
	task.wait(3)
end)

print("abc")

task.spawn creates a new thread so abc prints right away

1 Like

Use Tween.Completed:Wait() and destroy.

Tween.Completed:Once(function() cloud:Destroy() end)

or

task.delay(Tween.TweenInfo.Time, cloud.Destroy, cloud)

or

local Debris = game:GetService("Debris")


Debris:AddItem(cloud, Tween.TweenInfo.Time)

see Debris

Or you could just wrap your code with task.spawn for each cloud.

task.spawn(function()
  -- clone/position cloud
  -- tween cloud
  -- wait seconds or tween completion
  -- delete cloud
end)

Thank you! I ended up using task.delay as it was the easiest method to implement.

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.