How would I change properties with a table

I am unsure on how I would do this, and I have not tried anything so far.
An example of what I am doing with a function:

SetProperties = function(Object, Table)
    for index, c in pairs(Table) do
        Object[c.key] = c.Value
    end 
    --something like this, finds the property based on the keyname and sets the value

end

local a = Instance.new("Part")
SetProperties(a, {Name = "Test", BrickColor = BrickColor.new("Pastel Blue"), Anchored = true, Transparency = .5, Parent = workspace})

--reason for doing a for loop instead of specifically looking for properties is because I may also want to do it with other object:
local b = Instance.new("ScreenGui")
SetProperties(b, {Name = "UI", Enabled = false, Parent = game:GetService("StarterGui")})

Edit: Fixed title name which I managed to cut the end off

1 Like

Maybe try this?

function setObjectProperties(object, tableOfProperties)
   for propertyName, propertyValue in pairs(tableOfProperties) do
      object[propertyName] = propertyValue
   end
end
2 Likes

Thanks!, I forgot that index isn’t always a number :sweat_smile:

Yeah its okay. By the way, I strongly recommend you to use the traditional way of setting properties of objects:

local object = Instance.new(<object class name>) do
   object.<property name here> = <property value here>
   -- other properties...
   object.Parent = <objects parent here>
end

I think setting properties of objects with tables is a bad practice because it looks really weird and no one does it this way, though, I dont know why would you use tables for this.

replacing someone elses use of LoadLibrary(“RbxUtility”).Create() with a function that uses Instance.new() instead, I went through 100 odd lines just to make it look like function(“string”,{table}) instead of function"string"{table}, not the best way but it makes my job much quicker

EDIT: I made a function for this which does not modify how the syntax for rbxutility create was

	create = function(class)
		return function(properties)
			local ins = Instance.new(class)
			for key, val in next, properties do
				if type(key) == "number" and typeof(val) == "Instance" then
					val.Parent = ins
					continue;
				end
				ins[key] = val
			end
			return ins;
		end;
	end,
2 Likes