How would I create an custom function for objects (Instance:IncrementAttribute() for example)

Hello! I am currently wondering how it would be possible to use my current function:

local function IncrementAttribute(InstanceLocation, AttributeName, Incremented)
	InstanceLocation:SetAttribute(AttributeName, InstanceLocation:GetAttribute(AttributeName) + Incremented)
end

And do something like this:

InstancePart:IncrementAttribute("SwordDamage", 4)

Instead of

IncrementAttribute(InstancePart, "SwordDamage", 4)

If you can help, please let me know. Thanks! WE

You would want to wrap the instances. What I mean by wrap is provide an interface that you would interact with to interact with your instance.

Wait what would I do? I am very confused by wrapping? Are you talking about

coroutine.wrap

?

No I’m talking about constructing proxy instances which you interact with instead of interacting with the original object.

Here is an elementary wrapping function:

local function Wrap(Object, Interface)
    local Proxy = newproxy(true)
    local Meta = getmetatable(Proxy)

    function Meta:__index(Key)
        return Interface[Key] or Object[Key]
    end

    function Meta:__newindex(Key, Value)
        Object[Key] = Value
    end

    return Proxy
end

I could use this to wrap Workspace to give the interface of being a “SwagObject” and a “Workspace”

local SwagObject = {
    SwagFactor = 9999,
    WaffleCount = 123,
    PhD = "Kitten Sciences"
}

local Workspace = Wrap(game.Workspace, SwagObject)

print(Workspace.SwagFactor, Workspace.WaffleCount, Workspace.Gravity)

9999 123 196.2

This would not be worth using in my opinion because this would be extra code than all of the :IncrementAttribute()'s I would use in 1 script. Thanks anyway.

The point of making a complex wrapping function was so you could introduce more complex behavior later on.

This is all syntactic sugar though and honestly don’t worry over it.

You can use OOP (Object Oriented Programming) and ModuleScripts to achieve this.

local Module = {}

Module.__index = Module

function Module.new()
local NewModule = setmetatable({}, Module)
NewModule.RandomVariable = 5
return NewModule
end

function Module:PrintVariable()
print(self.RandomVariable)
end

return Module

Server Script

local OOP = require(script.OOP)
local OOPClass = OOP.new()

OOPClass:PrintVariable() -- Prints 5
1 Like

Ok. How would I increment an attribute with OOP’s.

Do something like the PrintVariable example.

local Module = {}

Module.__index = Module

function Module.new()
local NewModule = setmetatable({}, Module)
NewModule.RandomVariable = 5
return NewModule
end

function Module:IncrementAttribute(InstanceLocation, AttributeName, Incremented)
   InstanceLocation:SetAttribute(AttributeName, InstanceLocation:GetAttribute(AttributeName) + Incremented)
end

1 Like

Ok. Quick question. What is RandomVariable used for?

It’s not used for anything, you can exclude it. I was just giving you an example on OOP and how to use self

1 Like

So technically, I don’t need .new at all? I can just remove it?

And also, I would just do :IncrementAttribute like in the example on the topic?

Sorry, I only use Modules for storing info. Havent used them in a while since Attributes are a thing now.

The .new() function is made to make a ‘subclass’ in the Module, and making a new ‘subclass’ from the blueprint. Technically you can remove it, I guess.

And yes, I believe you would.

Hey! Got this error when requiring the Module:
image

local AttributeService = require(game.ReplicatedStorage:WaitForChild("AttributeService"))

I promise that the module exists in the area.
image

If needed, here is the modules code.
local Module = {}

Module.__index = Module

function Module:IncrementAttribute(InstanceLocation, AttributeName, Incremented)
	local IncrementedAttribute = InstanceLocation:SetAttribute(AttributeName, InstanceLocation:GetAttribute(AttributeName) + Incremented)
	return IncrementedAttribute
end

You forgot to type return Module at the end.

Wait, where would I return the module? Which script? I am guessing the ModuleScript right? But where in it?

Edit: Wait nvm

Ok. Now I get this error.
image

Player.InventoryData:IncrementAttribute("CurrentItems", 1)

Instead of Module:IncrementAttribute() in the module script, try self:IncrementAttribute

1 Like

image
Wait so it comes up as an unknown global when I enter it.

function self:IncrementAttribute(InstanceLocation, AttributeName, Incremented)

Sorry, as I said before, I used modules for info storing before Attributes.

Wait, nevermind. That wouldn’t work. Please show me your code (Script, not module)