Is it possible to add a function to a luau reserved table like “Instance”
For Example:
Instance.newPart = function()
local Part = Instance.new("Part")
return Part
end
Is it possible to add a function to a luau reserved table like “Instance”
For Example:
Instance.newPart = function()
local Part = Instance.new("Part")
return Part
end
No, you are prevented from doing this in order to stop you accidentally creating Spooky Action.
No, ever since table.freeze
was released, Roblox started freezing these tables. Before that, they were using metatables to achieve a similar read-only effect (this practice began around 2012, when they started taking the security of these APIs more seriously).
print(table.isfrozen(Instance)) --> true
If you really wanted to, you could overwrite the global variable with a new object that wraps the old table. However, the script editor (for good reason) will give you a warning if you attempt this.
do
local OldNew = Instance.new
Instance = setmetatable({
NewPart = function()
return OldNew("Part")
end
}, {__index = Instance})
end
print(Instance.NewPart()) --> Part
print(Instance.new("Part")) --> Part
This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.