Defining multiple parts in one script

Hello Developers!

I am still learning Lua, and I had a question when making the following: I have 3 parts in a baseplate, and I want to change their material to concrete all at once through a script in ServerScriptService. The parts are named "Colors". With the script I created below, am I able to change the "FindFirstChild" to something else in order to define all the parts under workspace?

local colors = game.Workspace:FindFirstChild("Colors")

colors.Material = Enum.Material.Concrete

Thanks!

EDIT: Found solution, thanks for the help!

3 Likes

You could use a for i, v loop to loop through all the children of the baseplate, but this is advanced.
I don’t remember how to do this right now, but there are some tutorials on YouTube about for loops in Lua on Roblox.

I would just make each part have a different name and make a separate variable for each part: ( If you are a programmer and see this, sorry for possibly burning your eyes. )

local partA = game.Workspace:FindFirstChild("partA")
local partB = game.Workspace:FindFirstChild("partB")
local partC = game.Workspace:FindFirstChild("partC")


partA.Material = Enum.Material.Concrete
partB.Material = Enum.Material.Concrete
partC.Material = Enum.Material.Concrete

Edit: I fixed the code, again. lol.

1 Like

Yes, I also knew this was a solution to my problem, however, I do plan on adding way more than 3 parts to the game so I need a more ‘simple’ script. I will try and do some more research for a loop (which I’m slightly familiar with).

Thanks for the help! I really appreciate it.

1 Like

Ah yes. Looping the same variable. Great code :slight_smile:
Edit:you fixed it

1 Like
for i, v in pairs(workspace:GetChildren()) do
	if v.Name == "Colors" then
		v.Material = Enum.Material.Concrete
	end
end
10 Likes

Tip for this code: put the parts in a folder then use the getChildren() on this folder. Otherwise it would be a virus xd

I’ll leave that to the original poster’s discretion, he mentioned the parts were in workspace so I made it specifically for that.

If I were to put the parts into a folder, would I put the name of the folder where it says FolderName? (workspace:GetChildren(FolderName)) do

No. It would be this: workspace.folder:GetChildren()

Alright that all makes sense now. Thanks for the help, I really appreciate this.

for i, v in pairs(workspace.Folder:GetChildren()) do
	if v.Name == "Colors" then
		v.Material = Enum.Material.Concrete
	end
end

Change the folder’s name to the name of the folder.