I am trying to figure out how to add an object to Instance.new() in roblox for my game. I didn't know where to go to find and edit them. The idea is that I would do Instance.new("Sword") and I could control what happens. If that is not a feasible option then please tell me and I will resort to writing my own instance script with oop. Please either refer me to a website, post or link that will show me where to go or just inform me on the best way to do this. Thanks!
With OOP, your best bet is probably just organizing your Sword
class, including the constructor, in a (or some) ModuleScripts.
As far as I know, I don't think there is a way to edit the Instance.new() function.
I don't believe Roblox allows access to the source code for their API, although I could be wrong. Anyhow, you could create a sort of wrapper around the Instance class using metatables/metamethods. They are pretty much the closest thing Lua has to OOP, and it's likely Roblox used them to make the objects in their API. Here's a link in case you don't know about them:
http://wiki.roblox.com/index.php?title=User:Thenewguy/How_does_ROBLOX_use_a_metatable
I mean, you could just make the sword a model then use LoadAsset instead of Instance.new.
You can overwrite the global Instance variable with your own table that indexes the real Instance class if necessary. You can then write your own 'new' function into the table.
local Instance = setmetatable({}, {__index = Instance}) local realNew = Instance.new Instance.new = function(name, parent) if script:FindFirstChild(name) then local instance = script[name]:Clone() -- Don't store stuff in the script, configure accordingly. instance.Parent = parent return instance end return realNew(name) -- exclude the parent argument as it is deprecated end Instance.new("Sword", workspace)
If you wrap that into a module you can simply do
local Instance = require(extendedInstanceClass)
at the beginning of all of your scripts.
This is a very simple way to extend other classes and libraries as well. Although, you do need to do a little extra playing with metamethods if you want to be able to call or write members of those extended classes. With the Instance class it doesn't matter though, as you will never be able to directly index and call Instance.FindFirstChild
for example.