PcoWSkbVqDnWTu_dm2ix
We use cookies on this site to enhance your user experience

Create Part Templates

Create Part Templates

Jun 23 2018, 2:02 AM PST

Problem

You want to create an object, set all its properties, and then use it throughout your code without setting it up over and over again.

Solution

Use the Clone method.

local frame = Instance.new('Frame')
frame.BackgroundColor3 = Color3.new(1, 1, 1)
frame.BackgroundTransparency = .5
frame.BorderSizePixel = 0
frame.Size = UDim2.new(.25, 0, .25, 0)
 
for i, v in ipairs(game.Players:GetPlayers()) do
	local lframe = frame:Clone()
	lframe.Position = lframe.Position + UDim2.new(0, i, 0, i)
	lframe.Parent = game.Players.LocalPlayer.PlayerGui.Screen
end

Discussion

First we created the Frame object and then set some of its properties. Then we had a loop and we cloned an instance of this Frame that had all its properties set, then changed its Position and Parent. This method of creating an object, and then using it later with the clone method is called templating (there isn’t an official name for this, I coined the term “templating” myself).

Tags:
  • optimization
  • template