How to wrap Roblox instances to allow for custom properties

scripting
#1

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…

XD

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!

12 Likes
closed #2

This topic was automatically closed after 1 minute. New replies are no longer allowed.

opened #3
#4

May I suggest that this would be even better if you can add a way to reference the instance from another script and get it’s wrapped information.

For example, Making a part wrapped with a Rarity property able to be accessed via another script with a WrappingModule:GetWrapped(Part part) or something.

Another suggestion would be using newproxy(true) for the return, because if you were to check the type(part), it would return "table" even though we know it’s a wrapped instance.

Really cool module nonetheless.

2 Likes
#5

This could be achieved by using ModuleScripts or other methods like_G, Shared & Bindables, but the getter would be a convenience to have.



@NYDynamics I like how you turned this into a module good job!

1 Like
#6

Interesting Ill try making that asap! I’ve uploaded the get wrapped part function in the module it looks through every service in the explorer!