Hello! I was wondering if it was possible to rewrite the functions for every of one type of instance, or default functions that are automatically in the game like print(). (For example: Rewriting player:GetMouse() to fire a remote event that gets the mouse in the client just in case it is called by the server.) The reason I want to do this is so that I can automatically fix old scripts that may not work due to updates so I don’t have to go in and manually fix it, which will save a lot of time and resources. This will especially help with the removal of LoadLibrary that will be happening soon.
Functions like print can be overwritten using module scripts. Some may not allow to be overridden.
local print = module.new()
Not without a wrapper, you can’t directly overwrite a function/method for Instances because they are attached as Metatables, and you can’t overwrite that MetaTable either.
You can however wrap the Instance inside a Table and work from there
This is what wrappers are for. Use metamethods to overwrite with custom functions or properties.
Opt for using separate functions instead of writing a wrapper, it’ll save you a lot of mind and time. GetMouseServer(player)
might not look as pretty but it’s good enough.
On a side note, does the Mouse instance even replicate?
I’m not sure actually. I know that you can fire the server with the mouse position but I’ve never really tried firing the mouse by it’s self. If you try and fire the mouse in an event it probably wont update the values when the client does.
No, it is received as nil through remotes.
I have an idea on how I can replicate the mouse to the server but I haven’t tried it yet. Basically the idea is to put a model into the player and then put the needed values inside that model and have a script update them when they change (example: mouse changes position). Then I can just wrap it to make player:GetMouse() get the model instead of trying to get a non existent mouse.
Changing the values on the client will not update them for the server. Either way, the server’s going to have to be sent the information at some point, so you might as well just send it when it’s needed through a remote.
There’s the option of changing environment variables but I don’t recommend it. Using a ModuleScript is far better. I believe getfenv also ignores Luau optimisations which is a downfall, but figure I post it anyway since it hasn’t been mentioned.
getfenv(0).LoadLibrary = function(libName)
return require(somethingSomewhere)
end
This changes the global LoadLibrary in the script’s global environment to a new function that implements require. The libraries should be somewhere in your game (preferably ReplicatedStorage) and the “somethingSomewhere” but should be changed to find a non-nil library and require it.
Sorry just want to note that functions can be overwritten without using a ModuleScript, but it is easier. Also, you cannot override keywords.
(thanks @colbert2677) for example, a few keywords you cannot override are function
, and local
.
That’s because those are keywords, not globals. Keywords cannot be overwritten in a script environment; globals can. Recall: a global is a variable that refers to something from the script’s default environment. They can be changed via get/setfenv (accessing the environment). Local variables have higher precedence.
I know that you have to use remote events. The reason I plan on using a model is because when you try to get the mouse on the server, it just returns nil regardless of weather it was sent in from an event or attempted to get from the :GetMouse() function.
EDIT: Also, the objective is building a system that basically fixes old scripts automatically so you don’t have to. That’s why I’m attempting to make a fake mouse, so if an old script from 2013 or earlier attempts to get the mouse in a server script, it will work without error.