Scripting Helpers is winding down operations and is now read-only. More info→
Ad
Log in to vote
0

Can I create a custom object able to be created by Instance.new?

Asked by 5 years ago

Recently I have really been looking to programming with OOP and realized - with some help from the community members here - that Instance.new is a form of OOP. After thinking for a while I thought I would ask if it was possible to create custom objects to create with Instance.new. I will be creating a blacksmith in my tycoon game that will allow players to customize their sword. By paying more they can get stuff like: Better animation, more damage, better looking sword and so on. Now I realize that it might be easier to Clone() things and change IntValues and require a module script or something on the sword. But I really think it would be cool if I could do:

Instance.new("Sword")
Sword.Damage = valueChosenByPlayer
Sword.Animation = animChosenByPlayer -- you get the idea

Even if I could do that, how would I get a model say in server storage be the actual sword that the player gets. Also, how would I access the Instance table or whatever to change it to accept: Instance.new("Sword"). If that wouldn't work I guess that I would have to use a module script and do Sword.new but still I am a little confused how I would get the numbers and the actual sword model chosen to work together. Finally, would it just be easier to Clone() the sword out of serverstorage and change IntValues inside of it than to do all this OOP stuff? If anyone can help I would really appreciate it. Thanks!

Note: I am not asking for you to write any script for me.

0
I think :Clone() is the best option you have for saving time, resources, and it's less of a headache Z0ggg 20 — 5y

1 answer

Log in to vote
1
Answered by 5 years ago

Well, honestly I don't think you can create a custom instance due to the fact we have :Clone().

It would actually be easier to clone then Instance mostly because :Clone() clones the properties as well while Instance.new() gives you the default properties, but you "Sword" isn't a part of Roblox's system so there is no "Default" property.

Plus, it would be not much in terms of differences, Instance.new() adds a object with default properties and I doubt you would want that so you probably would change the properties yourself.

Take a look:

local a = Instance.new('Part')
a.Parent = workspace

local b = workspace.Part:Clone()
b.Parent = workspace

The advantage here is if you have your properties ready, you don't even need to change much while Instance.new you would have to completely rewrite the properties.

In the end, a Custom Instance.new is basically a :Clone().

Hopefully, this helped you.

Best of luck developer!

Ad

Answer this question