Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
✓ ANSWER ACCEPTED

How can I access the Instance.new() base script in roblox?

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!

1 comment

0

This is not possible.

4 answers

✓ Accepted answer

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 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.

This discussion is preserved as it was. New answers, comments, and votes are closed.