Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
COMMUNITY QUESTION

Change the Size of multiple parts at the same time?

I have 4 parts that I want to each 'tween' their sizes every "X" amount of seconds.

This script tries doing so but I know I'm missing something. Instead this only gets one part in a Folder and only resizes that Part, then it moves on to the next Part:

local boundaryFolder = game.Workspace.Bounds

function increaseBoundaryRadius()
	local amount = 5	
	
	for i, v in pairs(boundaryFolder:GetChildren()) do
		if v.Name == "boundPart" then		
			for j = 0, 5, 1 do
				wait(1)
			 --this is where the parts should be increased every 1 second by adding 5 to their sizes
 
				v.Size = v.Size + Vector3.new(0, 0, amount)
			end
		end	

	end
end

while wait(7) do -- this is "X" amount of seconds
	print("Starting")
	increaseBoundaryRadius()
end

1 comment

0

Quick glance suggestion is to switch the for loops. Have the part loop nested inside the "tween" loop.

2 answers

The problem here is the way you have nested the loops. You get the individual items then per item you scale them not as a whole set.

This is a simple fix just swap the loops but this would be inefficient as we would get the children each time so it would be best to make a new table of parts we need to scale:-

local boundaryFolder = game.Workspace.Bounds

-- local functions need to be defined before they can be used
local function increaseBoundaryRadius(amountVector) 
	local tmpItems = {} -- store tmp items
	for _, v in paris(boundaryFolder :GetChildren()) do
		if v.Name == 'boundPart' then table.insert(tmpItems, v)  end -- add only the found items to the list
	end
	
	-- now the nested loop, we 1st start with the generic loop then we loop through each item in the new list to change the size

	for j = 0, 5, 1 do
		wait(1) -- we could have the wait time as another param in the function so we can change it

		-- change all items in the new list
		for _, itm  in pairs(tmpItems) do
			itm.Size = itm.Size + amountVector
		end
	end
end

while wait(7) do -- this is "X" amount of seconds
    print("Starting")
    increaseBoundaryRadius(Vector3.new(0,0,5)) -- we pass the vector as its param
end

I hope this helps please comment if you do not understand how / why this code works.

Use spawns. They're like coroutines bit a little bit "easier". This allows multiple threads to be ran without yielding. Lua runs code in a chronological order, and loops often yield (unless you use coroutines/spawns), so in the end:

local boundaryFolder = game.Workspace.Bounds

function increaseBoundaryRadius()
    local amount = 5    

    for i, v in pairs(boundaryFolder:GetChildren()) do
        if v.Name == "boundPart" then       
	spawn(function()
            for j = 0, 5, 1 do
                wait(1)
             --this is where the parts should be increased every 1 second by adding 5 to their sizes

                v.Size = v.Size + Vector3.new(0, 0, amount)
            end
	end)
        end 

    end
end

while wait(7) do -- this is "X" amount of seconds
    print("Starting")
    increaseBoundaryRadius()
end

Read up if you want more info about it:

http://wiki.roblox.com/index.php?title=Global_namespace/Functions_specific_to_ROBLOX#Spawn

This discussion is preserved as it was. New answers, comments, and votes are closed.