A quicker way to set/edit

As a ROBLOX Developer it is currently impossible to configure multiple variables at once, BUT, we can set multiple variables at once such as:

local Part1,Part2 = workspace.Part1,workspace.Part2

I already find this pretty useful but what if we could change a couple properties of those two parts at once?

Part1,Part2.Anchored = true,false
Part1,Part2.CanCollide = false,true
Part1,Part2.BrickColor = BrickColor.new('Really red'),BrickColor.new('Cyan')

Basically this is just a shortcut request that would make scripts a little shorter and the process a little quicker.

Doing what you suggest would cause more problems than what it would solve. Besides that, the syntax isn’t only weird, it would break syntax compatibility with regular Lua.

8 Likes

That would really screw up a lot of prior code and completely change what everyone’s been taught before. I think it’s fine as-is.

4 Likes

Lua already supports repeating operations without rewriting syntax. It’s called functions and loops.

If you’re manually setting the properties of many objects by name, chances are you’re not doing things very cleanly, which is causing you to be annoyed by this. What were you trying to do when you ran into this? We can probably show you a much cleaner way that will resolve the issue!

1 Like

I was actually watching the flash and thinking about lua for some odd reason.

Part1.Anchored, Part2.Anchored = true, false
Part1.CanCollide, Part2.CanCollide = false, true
Part1.BrickColor, Part2.BrickColor = BrickColor.new('Really red'), BrickColor.new('Cyan')

the code you wrote will overwrite the value of Part1 and change the properties of Part2. If it ever did anything other than that then it would be ambiguous syntax.

This inspired me to write a small thing for setting lots of properties or running many methods at once with syntax similar to what you’re looking for.

I don’t know if I’d ever actually use this. Too weird with too many hidden behaviors for me. I’ll probably stick with explicit loops. Here’s what the syntax looks like:

collect(Part1, Part2).BrickColor = collect(BrickColor.new('Really red'), BrickColor.new('Cyan'))
A more extended example
local Part1 = Instance.new("Part")
local Part2 = Instance.new("Part")

local parts = collect(Part1, Part2)
parts.Name = collect("Part 1", "Part 2")
parts.BrickColor = collect(BrickColor.new('Really red'), BrickColor.new('Cyan'))
parts.Transparency = 0.5
parts.TopSurface = "Smooth"
parts.BottomSurface = "Smooth"
parts.Anchored = true
parts.CFrame = collect(CFrame.new(2, 2, 0), CFrame.new(-2, 2, 0))
parts.Parent = workspace

wait(1)

parts:Resize(collect({Enum.NormalId.Top, 1}, {Enum.NormalId.Back, 2}))

wait(1)

parts:Destroy()

The programming behind collect
local collectMeta
collectMeta = {
	__newindex = function(this, key, value)
		if type(value) == "table" and getmetatable(value) == collectMeta then
			for i = 1, #this do
				this[i][key] = value[(i - 1)%(#value) + 1]
			end
		else
			for i = 1, #this do
				this[i][key] = value
			end
		end
	end,
	__index = function(this, key)
		return function(innerThis, val1, ...)
			if innerThis == this then
				if type(val1) == "table" and getmetatable(val1) == collectMeta then
					for i = 1, #this do
						this[i][key](this[i], unpack(val1[(i - 1)%(#val1) + 1]))
					end
				else
					for i = 1, #this do
						this[i][key](this[i], val1, ...)
					end
				end
			elseif type(innerThis) == "table" and getmetatable(innerThis) == collectMeta then
				for i = 1, #this do
					this[i][key](unpack(innerThis[(i - 1)%(#innerThis) + 1]))
				end
			else
				for i = 1, #this do
					this[i][key](innerThis, val1, ...)
				end
			end
		end
	end
}
local function collect(...)
	return setmetatable({...}, collectMeta)
end

return collect

Some neat things about collect:

  • You can set all properties to the same thing using .Property = value
  • You can set all properties to different things with .Property = collect(value1, value2, ...)
  • You can set all properties such that it’s a repeating pattern with .Property = collect(value1, value2). For the third object, it will use value1, for the fourth, value2, for the fifth, value1, etc.
  • You can call methods with the same features as above
  • You can call methods referencing the objects using :Method(...)
  • You can call methods not referencing the objects using .Method(...)
  • You can get the values back using unpack(collection)
    • e.g. Part1, Part2 = unpack(parts)
  • You can loop through the collection just like you would a regular list.

Lua is pretty flexible, isn’t it?

7 Likes