Is it possible to make own Instances?

You can write your topic however you want, but you need to answer these questions:

  1. What do you want to achieve? Keep it simple and clear!
    I want to make a custom instance, with them i can see it into the studio

  2. What is the issue? Include screenshots / videos if possible!
    I don‘t know how to attempt this. I have ask may time ago how to make a custom class, but i don‘t know how to show it into studio.

  3. What solutions have you tried so far? Did you look for solutions on the Developer Hub?
    This was sure not in the Developer Hub, this is why i am asking here.

Here is the Link about my custom class topic: How do you make custom good class?

And if possible i want to know how to make my own Instance.new(). I want to make this as i am working in a card system, like Yu-Gi-Oh or Battle spirits, and if i use a script for all card then it will sure cause that the game crash. And yes, i know the CollectionService, but how can i use it if i not can make that my class inherits from the Instance, as it need a Instance in the parameters? And i am asking me how to make a class inherits one other class in Roblox, as it was currently impossible. Sorry if this was long, i hope you understand…

~~Eterna

2 Likes

No, this isn’t possible to do. Roblox doesn’t support class creation. Custom classes done through ModuleScripts are pseudoclasses; they follow the same tenets as any standard Roblox object, but you can’t represent them as DataModel objects nor claim any of their benefits.

5 Likes

@colbert2677 why? I understand that Roblox don‘t want that Hackers make custom virus classes or something else. Then can you answer my second question?

You coould make a wrapper for Instance.new. But then it wouldn’t make sense because your classes don’t actually inherit from Instance. Please don’t.

1 Like

@incapaxx, how to make that my classes inherit from Instance? In Java we can simply use the „extends“ keyword, but i don‘t know how to make it in Roblox… And what is a wrapper?

You can’t. And you don’t. You would probably have to hardcode each method, and even then it’s a copy at that point.

A wrapper usually just adds on to a class, or to a function.

2 Likes

Ok, and it‘s possibly to make that when i am using the Instance.new(String Class, Instance Parent) Function that when i am using in the parameters my own class that it then work (i mean that it create a class/instance)?

Is it possible to do something like this in Roblox (i.e. yourself)?

Please, don’t. But here is a way.

Assuming you have a hierarchy like this, this can work.

image

local new_instance = Instance.new
local custom_classes = game:GetService("ReplicatedStorage").CustomClasses
local Instance = {
    new = function(class_name, ...)
        if custom_classes:FindFirstChild(class_name) then
            return require(custom_classes[class_name]).new(...)
        end
        return new_instance(class_name)
    end
}

Pretty much, if the module is a custom one, redirect to that, otherwise just use a regular roblox class. Do note, this doesn’t make it inherit from instance. You would have to hardcode that.

local new = Instance.new("Pizza", "Pepperoni", 8)
new:Eat()
print(new.Slices) -- 7
local new = Instance.new("Hotdog", "Chicago Bun")
new:AddCondiment("Ketchup")
print(new.Condiments[1])
If you were wondering what the codes for those actually were...
-- Hotdog class example
local Hotdog = { }
Hotdog.__index = Hotdog

function Hotdog.new(bun_type)
	return setmetatable({ Condiments = { }, BunType = bun_type })
end

function Hotdog:AddCondiment(condiment)
	table.insert(self.Condiments, condiment)
end

return Hotdog
-- Pizza class example
local Pizza = { }
Pizza.__index = Pizza

function Pizza.new(flavor, slice_count)
	return setmetatable({ Flavor = flavor, Slices = slice_count }, Pizza)
end

function Pizza:Eat()
	self.Slices = self.Slices - 1
end

return Pizza
8 Likes

I am confused, so the new_instance was the same as Instance, but here was still was something that i am wondering: what instance do you mean? And what is hardcode? Is this the same as the Overload in Java? But thanks for this good reply @incapaxx !

What hardcoded means

Lua doesn’t have function overloading, though it does have operator overloading.

which java
doesn’t have
smh

But the idea is the same, yes. The function’s behavior changes based on the class name (though overloading is usually based on types)

2 Likes

Thank you for the short and concise answer!!