How would I turn this code into a function?

So basically Im trying to create a part after the part I specify to be cloned, but then I clone the clone of that part and then move it accordingly

I dont want to have do this for every single part I want to spawn and position after the last one

for i = 1, 1 do
	local gridPart = part:Clone()
	gridPart.Parent = workspace
	gridPart.Position = gridPart.Position + Vector3.new(0,0,-part.Size.Z)
	for i = 1,1 do
		local gridPart2 = gridPart:Clone()
		gridPart2.Parent = workspace
		gridPart2.Position = gridPart2.Position + Vector3.new(0,0,-part.Size.Z)
		for i = 1,1 do
			local gridPart3 = gridPart2:Clone()
			gridPart3.Parent = workspace
			gridPart3.Position = gridPart3.Position + Vector3.new(0,0,-part.Size.Z)
		end
	end
end

Result:

local function setPartClone(part)
	local gridPart = part:Clone()
	gridPart.Parent = workspace
	gridPart.Position = gridPart.Position + Vector3.new(0,0,-part.Size.Z)
	
	return gridPart
end

And when you need to use it, just reference the part you want to clone, in this case

for i = 1, 1 do
	local gridPart = setPartClone(part)
	for i = 1,1 do
		local gridPart2 = setPartClone(gridPart)
		for i = 1,1 do
			local gridPart3 = setPartClone(gridPart2)
		end
	end
end

This should hopefully work

Alright Ill try this out and get back to you thanks

Alright, if anything isn’t working or something wasn’t explained well, just send a reply and I’ll get back to you!

erm, it didnt work and it just places them in the same place

That’s odd, I’ll try to recreate what you’re trying to do and get back to you once I find how it’s actually supposed to be

Yeah like it creates one of the parts inside of the very first one and then positions the other one weirdly in front of one of them

Okay so I made a rough method of what you’re trying and for some reason it’s working for me without any changes, what’s happening when you try it? Could you show me a picture

wait for some reason it just now worked so maybe i typed something in wrong

But is there any way to make this even more robust?

What do you mean by more robust?

Like less work on my end, I want to have a loop that does all this for me without me having to actually type in the clone and yadayda

I dont want to have to do this

for i = 1, 1 do
	local gridPart = setPartClone(part)
	for i = 1,1 do
		local gridPart2 = setPartClone(gridPart)
		for i = 1,1 do
			local gridPart3 = setPartClone(gridPart2)
		end
	end
end

All the time

I want to make the script know what the last part was and do it accordingly but I just cant wrap my head around how I would do that

The simplest way of doing that is this

for i = 1, 4 do
	part = setPartClone(part)
end

The Simplest way to explain it is. Set part down and return that part for the part variable, and do that 3 more times

Edit: I already did, I should’ve mentioned that, sorry

Can you test and see if it works please

Did it work though?

Could you show the whole script please Im a beginner at this stuff

I did this

local part = workspace.Part

local function setPartClone(part)
	local gridPart = part:Clone()
	gridPart.Parent = workspace
	gridPart.Position = gridPart.Position + Vector3.new(0,0,-part.Size.Z)

	return gridPart
end

for i = 1, 4 do
	part = setPartClone(part)
end

Just replace workspace.Part with your part

1 Like

Omg this is literally amazing thank you so much

1 Like

Anytime! If you have anymore issues don’t be afraid to make another post!

1 Like

Instead of doing all of that, can’t you do something like

local amountX = 30
local amountZ = 30

local startingPosition = Vector3.new(0,.3,0)
local colors = {Color3.fromRGB(128, 187, 219),Color3.fromRGB(84, 123, 144)}
local colorPoint = 1
for x = 1,amountX do
	for z = 1,amountZ do
		if colorPoint >= #colors then
			colorPoint = 1
			else
			colorPoint +=1
		end
		local part = game.ReplicatedStorage.gridPart:Clone()
		part.Position = startingPosition + Vector3.new(x*part.Size.X,0,z*part.Size.Z)
		part.Color = colors[colorPoint]
		
		part.Parent = workspace
		
	end
	
	
	if colorPoint >= #colors then
		colorPoint = 1
	else
		colorPoint +=1
	end
	
end