Tweening and destroying clones of ImageLabels on completion of tweens

I’m making a script that creates bullet holes when the player is shot, and then shrinks them after a while before destroying them (yes i know its currently in a while loop)

the problem, is that the script is waiting for the tweens to be completed and for the bullet holes to be destroyed before creating another bullet hole, I need it to continue creating bullet holes regardless of whether the last one is done tweening or being destroyed or not.

I’ve tried destroying and creating them in seperate functions, and I’ve searched everywhere on the forums.

Any help is appreciated!

local Player = game.Players.LocalPlayer
local Character = Player.Character or Player.CharactedAdded:Wait()
local Humanoid = Character:WaitForChild("Humanoid")
local tweenService = game:GetService("TweenService")

function CloneHoles()
	while true do
		wait(2.5)
		print("Hole Created!")
		HoleClone = script.Parent:WaitForChild("BulletHole"):Clone()
		HoleClone.Parent = script.Parent
		HoleClone.Position = UDim2.new(math.random(0,9)/10,0,math.random(0,8)/10,0)
		HoleClone.Rotation = math.random(0,359)
		HoleClone:TweenSize(UDim2.new(0.15, 0, 0.225, 0),Enum.EasingDirection.Out,Enum.EasingStyle.Elastic,0.5,true)
		wait(1)
			HoleClone:TweenSize(UDim2.new(0.0, 0, 0.0, 0),Enum.EasingDirection.InOut,Enum.EasingStyle.Linear,5,true)
		wait(5)
			HoleClone:Destroy()
	end
end

CloneHoles()

You can do a few things that might work. I think the wait()s are holding the script up. You could try replacing the TweenSize() with a normal Tween(), since that has a delay function you can use, and it should wait to play the tween. Something like this:

local tweenInfo = TweenInfo.new(
	2, -- Time
	Enum.EasingStyle.Linear, -- EasingStyle
	Enum.EasingDirection.InOut, -- EasingDirection
	0, -- RepeatCount (when less than zero the tween will loop indefinitely)
	false, -- Reverses (tween will reverse once reaching it's goal)
	1 -- DelayTime
)

local tween = TweenService:Create(HoleClone, tweenInfo, { Size = UDim2.new(0.0, 0, 0.0, 0) })

tween:Play()

…and have them play at the same time, the other playing a second later because of the delay.
Then, you can use Roblox’s Debris service to remove it after 5 seconds, without making the script yield for anything:

local DebrisService = game:GetService("Debris")
DebrisService:AddItem(HoleClone, 5)

Here are some of the documentation pages on them if you wanna read some more in case something fails:

Well that solves half my problem, but I need the shrinking tween to have a longer time than the growing tween.

You could make multiple tweenInfos and multiple tweens, like this (I removed the comments from the tweenInfos to save on space):

local tweenInfoGrow = TweenInfo.new(0.5, Enum.EasingStyle.Elastic, Enum.EasingDirection.Out, 0, false, 1)
local tweenInfoShrink = TweenInfo.new(5, Enum.EasingStyle.Linear, Enum.EasingDirection.InOut, 0, false, 1)

local growTween = TweenService:Create(HoleClone, tweenInfoGrow, { Size = UDim2.new(0.15, 0, 0.225, 0) })
local shrinkTween = TweenService:Create(HoleClone, tweenInfoShrink, { Size = UDim2.new(0.0, 0, 0.0, 0) })
2 Likes

Does this method also allow for there to be a buffer between when it gets done growing, and when it starts shrinking? (Sorry for the amount of questions, I haven’t learned much of lua or python)

It should! Just increase the DelayTime (the last number in that tweenInfo list) of the tweenInfoShrink, if that’s what you’re asking. I’m assuming the shrink tween is supposed to be played last, so when you play them both at the same time, the shrink tween should be delayed by however many seconds you put, making a buffer.

(There’s also probably a better way of going about this altogether, that while true do loop might be messing with stuff. But this is just what I’m thinking off the top of my head)

TweenSize actually has a callback feature that you could use.

HoleClone:TweenSize(UDim2.new(0.15, 0, 0.225, 0), Enum.EasingDirection.Out, Enum.EasingStyle.Elastic, 0.5, true, function()
	wait(0.5)
	HoleClone:TweenSize(UDim2.new(0, 0, 0, 0), Enum.EasingDirection.InOut, Enum.EasingStyle.Linear, 5, true, function()
		HoleClone:Destroy()
	end)
end)

Tried this, need a longer buffer between grow and shrink tween, and call back for some reason is destroying instantly without tweeting

Using this i was able to call the tween.completed event. thanks!