so i was wondering if there is a way to list every part in a model with a script and doing something with them (i wanna turn all the parts in a model to a different material) and if its possible change the brightness of a pointlight in it.
Yes you could use for example,
local parts = {}
for i,v in pairs(workspace:GetChildren()) do
if v.Name == "ColorPart" then
table.insert(parts, v)
end
end
--to create a table:
local Table = {}
or maybe
local Table = {
hello = "hi"
}
print(Table.hello)
or _G["TableAccessedByAllScripts"] = {}
ok thank you very much for you help
heres an example that changes all of the parts in a model to the neon material and sets the brightness of a pointlight:
local model = game.Workspace.Model --path to your model
local parts = model:GetDescendants() --gets a list of all the objects in your model
for i = 1, #parts do
if parts[i]:IsA("BasePart") then --makes sure it's a basepart so the script doesn't break.
parts[i].Material = Enum.Material.Neon --or whatever you want the material to be.
end
if parts[i]:IsA("PointLight") then -- make sure it's a pointlight so the script doesn't break.
parts[i].Brightness = 5 --higher value means higher brightness
end
end
There are a lot of different ways to create, minuiplate and iterate tables in lua/roblox and they can be found through simply searching, there is a whole article on them right on the dev hub/wiki and there is api reference that includes GetChildren
and GetDecedents
GetChildren:
GetDescendants:
Tables:
Examples of ways to create tables:
table.create()
local Table = {}
local Table
Table = {}
Examples of ways to add values to a table:
table.insert()
Table["NewIndex"] = 5
And finally iterators:
I hope the tutorial isn't that bad
inserts a message using table.insert()
the first loop is getting the list of messages then passing it onto the maTable table.
local maTable = {}
local listed = {"hi"}
for _, v in pairs(listed) do
if v.Name == "hi" then
table.insert(maTable, v)
end
end
for i,v in pairs(maTable) do
print(v) -- prints the string in the table
end
To alter the constituents of an array (of a model’s parts for example )
this code would loop through a model’s children and check whether each child is a point light, if so then it’s brightness is changed to 0. If it’s a part then it’s material is changed. There is no need at all to list them in table (however i’ve provided the code to do that too).
Script parented to the part (Server Script)
Will also print what changed to what :
local model = workspace:WaitForChild("Model")
local model = workspace:WaitForChild("Model")
for _, child in ipairs(model:GetChildren()) do
if child:IsA("PointLight") then child.Brightness = 0--change it here
return print("brightness of light "..tostring(child.Name).."set to 0")
end --if the model's child is not a light then it's material is changed instead
child.Material = Enum.Material.Neon
print("changed material of child of model "..tostring(child.Parent).." to "..tostring(child.Material))
end
Output :
If you want to insert
all children of a part into a dictionary, with the key being the number of the part and the value being it's name, you would do this : local dictionary = {
[1] = "Part type"
}
local model = workspace:WaitForChild("Model")
for key, child in ipairs(model:GetChildren()) do--for how many children exist, loop
dictionary[child.Name] = child.ClassName
print(key,child)
end
Output :
To simply change a model’s childrens’ material loop like this :
local model = workspace:WaitForChild("Model")
for _, child in ipairs(model:GetChildren()) do
child.Material = Enum.Material.Brick
end
To insert children into a table :
tablex = {}
for _, child in ipairs(model:GetChildren()) do
child.Material = Enum.Material.Brick
table.insert(tablex,child:Clone())--Inserting them children
end