Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
✓ ANSWER ACCEPTED

Having a property value automatically set when a new instance is made?

I'm making tons of TextButtons for a game I'm making and for almost all of them I want the TextWrapped, BackgroundColor White, and textcolor black.

Is there any way to set what the values of properties in instances when they are immediately created without having to individually set the property for every single one I make in my scripts?

3 answers

✓ Accepted answer

If you want to be able to use Instance.new() normally without having to make a bunch of edits, you can just wrap the function:

do
	local old = Instance;
	local newInstance = {};
	for name, func in next, old do
		if name ~= 'new' then
			newInstance[name] = func;
		end;
	end;
	
	newInstance.new = function(name, parent)
		if name == 'TextButton' then
			local new = old.new(name, parent);
			new.TextWrapped = true;
			new.TextColor3 = Color3.new(0, 0, 0);
			new.BackgroundColor3 = Color3.new(1, 1, 1);
			return new;
		else
			return old.new(name, parent);
		end;
	end;
	
	Instance = newInstance;
end;

local button = Instance.new('TextButton') --// This would be a TextButton with those properties already set.

Hope this helped.

Make a base Instance in ServerStorage or ReplicatedStorage, then use the :Clone() function to duplicate it, i.e.

local baseTextButton=game.ServerStorage.TextButton

--Set your base properties here, i.e.
baseTextButton.Text="Hello World!"

local buttonOne=baseTextButton:clone()
local buttonTwo=baseTextButton:clone()
local buttonThree=baseTextButton:clone()
local buttonFour=baseTextButton:clone()
local buttonFive=baseTextButton:clone()

You can use a for loop to create multiple instances and set the properties, then put them into an array, to modify later.

local numberOfButtons = 3
local Buttons = {}
local YourFrameHere = Instance.new("Frame", screenGUI)
--Create the buttons
for i = 1, numberOfButtons do
	Buttons[i] = Instance.new("TextButton", YourFrameHere) 
	Buttons[i].TextWrapped = true 
	Buttons[i].TextColor3 = Color3.new(0,0,0) 
	Buttons[i].BackgroundColor3 = Color3.new(255,255,255)	
	Buttons[i].Size = UDim2.new(.1,0,.1,0) 
end
--Then edit them to be different
Buttons[1].Text = "Button1"
Buttons[2].Text = "Button2"

You can set as many properties as you like for each button, following the same sort of pattern, then individually modify them.

This discussion is preserved as it was. New answers, comments, and votes are closed.