Tween all descendants at the same time

Hello! I am working on an item-pickup system and I tween all parts of an item (its is a wood block with some branches, so its a model with a primarypart) but when I tween them the game waits for the first part to tween and only then tweens to the second, then waits again and tween the third, etc… How do I tween all of the parts at the same time?
code:

pickupItem.OnServerEvent:Connect(function(player, item)
	local character = player.Character
	local inventory = player.Inventory
	if item then
		print("found item")
		local itemValue = inventory:FindFirstChild(item.Name)
		if itemValue then
			print("item is in folder")
			itemValue.Value = itemValue.Value + 1
			
			local finishingLocation = character.HumanoidRootPart.CFrame 
			
			local propertiesToTween = {Transparency = 1, Size = Vector3.new(0,0,0), CFrame = finishingLocation}
			
			for i, part in pairs(item:GetDescendants()) do
				if part:IsA("BasePart") then
					part.CanCollide = false
					part.Anchored = true
					local tween = TweenService:Create(part, pickupTweenInfo, propertiesToTween)
					tween:Play()
					wait(0.3)
					print("finished tweening "..part.Name..", which is a descendant of "..item.Name)
				end
			end
			
			item:Destroy()
		end
	end
end)
1 Like

There’s your issue, it’s yielding after every Tween.
Instead of using wait(), which can be unpredictable, Connect on Tween.Completed that of course is asynchronous.

1 Like

Ohh alright, thanks. I didn’t know that that feature existed.

1 Like

Alright, so I don’t really understand how I would add it to the script, as every time i becomes greater by 1, a new “local tween” variable is declared

Replace the wait() for your Connection, you can put the print inside the Connected function:

tween.Completed:Connect(function()
    print(string.format("Finished tweening %s, which is a descendant of %s", part.Name, item.Name));
end)
2 Likes