How to Wrap Instances in a Script
When I started developing Wrapping Instances wasn’t easy for me in the beginning until I fully understood it. That is why I want to share this with the community.
First, before we can wrap an instance we need to know what wrapping is. Wrapping is a way to add additional properties to instances that are not already properties. For instance, there is a way to set a birth time property to an instance by using wrapping.
Prior Knowledge:
Before you start wrapping instances you must know…
- How to create an instance
- How to create a Function
- How to create a Variable
- Meta Tables
The first thing we have to do is create the instance. In this case it will be a Part that will be a child of the workspace.
local Part = Instance.new("Part", workspace)
This creates a part inside our game and sets its parent to the workspace ^^^.
Now we are going to create our function which is quite simple.
function WrapPart(part)
This creates a function called wrap part^^^
Now we will setup the rest of the function
function WrapPart(part)
local interface = {} --defines interface
local behavior = {} --defines behavior
interface.Time = tick()--Creates a property called time which is equal to tick()
behavior.__index = part --sets behaviors index to part
function behavior:__newindex(key,value)
part[key] = value
end
return setmetatable(interface,behavior)
end
Now we can call our function
local WrappedPart = WrapPart(Part)
print(WrappedPart.Time)
We can also use wrapping for many more uses for instance we can also create a property BirthTime
function WrapPart(part)
local interface = {} --defines interface
local behavior = {} --defines behavior
function WrapPart(part)
local interface = {} --defines interface
local behavior = {} --defines behavior
interface.BirthTime = os.time()--Creates a property called Birthtime which is equal to os.time()
behavior.__index = part --sets behaviors index to part
function behavior:__newindex(key,value)
part[key] = value
end
return setmetatable(interface,behavior)
end
If you print the property created time, birthtime…etc you should return its value as a property ex:
local Part = Instance.new("Part", workspace)
function WrapPart(part)
local interface = {} --defines interface
local behavior = {} --defines behavior
interface.Time = tick()--Creates a property called time which is equal to tick()
behavior.__index = part --sets behaviors index to part
function behavior:__newindex(key,value)
part[key] = value
end
return setmetatable(interface,behavior)
end
wait(3)
local WrappedPart = WrapPart(Part)
print(WrappedPart.Time)
If you look at the output you should get your property’s value…
As you see I got 15574… etc
Now it’s your turn!
Wrapping uses
There are many useful ways to use wrapping. For instance it can be used to script GUI’s if your using its birth time. Like tweening a GUI a certain time after an instance was created.
If you’re still confused you can use my module for wrapping instances…
Thank You!