How to change a part properties using a table

Hi, I am trying to make a more efficient way of changing a parts properties:

Script:

local Bullet = Instance.new("Part")
	Bullet.Material = "SmoothPlastic"
	Bullet.Parent = game.Workspace
	Bullet.Transparency = Transparency
	Bullet.Anchored = true
	Bullet.CanCollide = false
	Bullet.TopSurface = Enum.SurfaceType.Smooth
	Bullet.BottomSurface = Enum.SurfaceType.Smooth

Is there anyway to use a table to change a part’s properties?

1 Like

Are you looking for something like this?

3 Likes

Do u mean smthing like this?

local table = {
    Material = Enum.Material.Neon,
}

for key, value in pairs(table) do
    part[key] = value
end
4 Likes

Yes, thank you very much @Downrest @aliaun125

Let me explain how this works.

It basically loops through the table, then checks if the index (e.g. ‘Anchored’) is found as a property in the part. If it is, set the value (e.g. true) of the index to the part’s property.

2 Likes

Oh now I get it, It goes through each property and changes the value if the property is found.

1 Like

Original author of the quoted reply here. While this functions does the job as intended, I would recommend adding additional code to make sure Parent property is assigned at the very end to make sure it’s not slowed down by extra internal event work when changing properties inside the for loop:

function module.CreatePart(parameterTest1)
   local partNew = Instance.new("Part")
   local parent = parameterTest1.Parent
   
   parameterTest1.Parent = nil
   
   for PropertyName, Value in pairs(parameterTest1) do
      partNew[PropertyName] = Value
   end

   partNew.Parent = parent
   parameterTest1.Parent = parent
end

--[[SCRIPT]]--
local dictionaryProperty = {
   ["Anchored"] = true,
   ["CanCollide"] = false,
   ["Name"] = "testPart"
}

module.CreatePart(dictionaryProperty)
1 Like