You can write your topic however you want, but you need to answer these questions:
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
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.
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.
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…
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.
@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?
@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?
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)?
Assuming you have a hierarchy like this, this can work.
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
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 !