Attempt at creating simultaneous tweens for multiple parts

I’ve been currently been experimenting with tweenservice a bit by making some parts rise out of the ground to resemble buildings; I’ve been trying to get parts that need to be columns to get resized properly altogether at once using this piece of code right here:

for i,v in pairs(workspace.Columns:GetChildren()) do
		coroutine.wrap(function()
			local coltween = TweenService:Create(v, tweenInfo, {Size = Vector3.new(5,308,5)})
			coltween:Play()
		end)
		end

Mind you, just about everything else has already been executed by the script at this point regarding proper parenting and part sizing/positioning and unfortunately this line of code isn’t working for some reason. Everything else beforehand works fine until this executes (or rather doesnt execute at all technically) and ends up with no errors in the output. The script is regular and not a local one. Here’s where the column parts that need resizing are in the workspace by the time it runs through if anyone cares:
image
Does anyone have any solution to this?

2 Likes

Hey there! :wave:

It seems you don’t have a variable for the TweenService, ensure you create one which may fix the issue you’re experiencing! You can do so by copying the script provided below and pasting it outside of the main code. :grinning_face_with_smiling_eyes:

local TweenService = game:GetService("TweenService")

Let me know if this fixes the issue. Best of luck with the progress of your game! :heart:

I actually have already created the variable for tweenservice already throughout the script; if it wasn’t already clear, all parts that were created in the process of structure construction through the script was resized through using tweenservice. If it helps you any this is the entire bit of code so far:

while true do --city gen v0.0.5
wait(1)
local TweenService = game:GetService("TweenService")
local genchance = math.random(1,10)
local positioningx = math.random(-500, 500)
local positioningz = math.random(-500, 500)
local tweenInfo = TweenInfo.new(
	5, -- Time
	Enum.EasingStyle.Linear, -- EasingStyle
	Enum.EasingDirection.Out, -- EasingDirection
	0, -- RepeatCount (when less than zero the tween will loop indefinitely)
	false, -- Reverses (tween will reverse once reaching it's goal)
	0 -- DelayTime
)
if genchance == 10 then
	print("city generated at x = "..positioningx.." and z = "..positioningz)
	local foundation = Instance.new("Part", game.Workspace)
	local capitalmain = Instance.new("Part", game.Workspace)
	local column = Instance.new("Part", game.Workspace)
	local cmodel = Instance.new("Model", game.Workspace)
	local main = Instance.new("Model", game.Workspace) --capital model
	cmodel.Name = "Columns"
	main.Name = "Capital"
	for _, v in pairs(workspace:GetChildren()) do
		if v:IsA("BasePart") then
			v.Anchored = true
		end
		end
	foundation.Material = "Concrete"
	capitalmain.Material = "DiamondPlate"
	column.Material = "DiamondPlate"
	
	foundation.Name = "CapitalFoundation"
	capitalmain.Name = "TowerPart"
	column.Name = "Column1"
	column.BrickColor = BrickColor.new("Dark stone grey")
	column.Size = Vector3.new(1,1,1)
	for i = 2,4 do
		local newcolumn = column:Clone()
		newcolumn.Parent = workspace.Columns
		newcolumn.Name = ("Column"..i)
	end
	column.Parent = workspace.Columns
	foundation.Position = Vector3.new(positioningx, 0, positioningz)
	capitalmain.Position = Vector3.new(positioningx, 0, positioningz)
	
	workspace.Columns.Column1.Position = Vector3.new(positioningx + 20, -2, positioningz + 20)
	workspace.Columns.Column2.Position = Vector3.new(positioningx - 20, -2, positioningz - 20)
	workspace.Columns.Column3.Position = Vector3.new(positioningx - 20, -2, positioningz + 20)
	workspace.Columns.Column4.Position = Vector3.new(positioningx + 20, -2, positioningz - 20)
	
	local tween = TweenService:Create(foundation, tweenInfo, {Size = Vector3.new(60,1,60)})
	local tween2 = TweenService:Create(capitalmain, tweenInfo, {Size = Vector3.new(40,300,40)})
	
	tween:Play()
	tween2:Play()
	wait(5)
	for i,v in pairs(workspace.Columns:GetChildren()) do
		coroutine.wrap(function()
			local coltween = TweenService:Create(v, tweenInfo, {Size = Vector3.new(5,308,5)})
			coltween:Play()
		end)
		end
	foundation.Parent = main
	capitalmain.Parent = main
	cmodel.Parent = main -- descend all existing structure parts/models into capital model
end
end
1 Like

Ah, I see. Unfortunately, I’m unsure of how to fix this issue but I’m sure there’ll be someone else out there who has adequate knowledge to provide a solution for the issue you’re experiencing! My deepest apologies! :frowning_face:

Hey!
I am not extremely experienced with corountines, but from what I’ve read, a coroutine.wrap will not resume until you call it (like a function).

So you could try something like this:

for i,v in pairs(workspace.Columns:GetChildren()) do
	local create = coroutine.wrap(function()
		local coltween = TweenService:Create(v, tweenInfo, {Size = Vector3.new(5,308,5)})
		coltween:Play()
	end)
	
	create()
end

What I do a lot when it comes to coroutines is:

coroutine.resume(coroutine.create(function()
	local coltween = TweenService:Create(v, tweenInfo, {Size = Vector3.new(5,308,5)})
	coltween:Play()	
end))

Runs it immediately without any further calls needed.

2 Likes

Just to add, tweens actually play right away and do not yield until the action is finished. You can call Play and continue with your script like nothing is happening, so no need for coroutines at all. In your case the loop will create and play all four tweens in the same moment.

for i,v in pairs(workspace.Columns:GetChildren()) do
	local coltween = TweenService:Create(v, tweenInfo, {Size = Vector3.new(5,308,5)})
	coltween:Play()
end
3 Likes

i appreciate the assistance :slightly_smiling_face: