Description
Recently when requiring a module from the command line in studio and calling a function from said module, my command line ran an old version of the function. That is, I edited a few lines of my module and the command line still ran the function as it was before the edits. I do not believe this is expected behavior.
Reproduce Steps
Create a ModuleScript in ServerScriptService
Write any function in it
Run that function in the Roblox Studio Command Line by requiring the module and calling the function
Open the ModuleScript and edit the function
Run the function again in the Roblox Studio Command Line by requiring the module and calling the function (the old function will still be run)
*Note: the most easy reproduction would be to have a function that prints a statement and just edit the statement
*Another Note: I thought maybe publishing the place in between command line calls to the function might fix it, but I was wrong
Specifically, my module’s function parented a Motor6D instance to the character’s HumanoidRootPart. I edited the code to parent the Motor6D instance to the character’s UpperTorso. After making these changes, the Motor6D instance still appeared under the HumanoidRootPart after edits were made and I re-ran the command line instructions.
I believe the appropriate behavior would be for the Command Line tool to run the most up to date module.
I am sure this is related to the fact that ModuleScripts cache, i.e. they only run once for performance purposes. They always return the same result in the same server.
-- module script
return math.random(1,100)
-- script
print(require(module)) -- 73
For every script in the game that requires the module, it will always be 73 until the game restarts and the module reloads, no matter how many times you require it.
You can probably solve this by cutting and pasting the module, but I agree that it is not ideal.
You can clone the module before you require it, which in my opinion is a bit nicer, if you don’t rely on Parent property of script inside of that module:
Thanks @JarodOfOrbiter and @guidable! While these are not ideal solutions, I realize now that this may not be a studio bug as much as a disliked behavior. I wish there was some sort of refresh icon to lex/parse/compile the ModuleScript instance over again.
The only way to test the module without cloning it or cut/pasting it would be to actually run a studio test session and call the function I edited. This is annoying because I don’t want to do this every time I make a 1-line change. I wish I could just require it in the command line, run it, and see what happens after the 1-line change. Ideally, the command line would run the code that’s in the published / production source code rather than the cached version made at the last compilation time.
P.S. - @Subcritical_alt moved this thread to #platform-feedback:studio-features now so maybe this will show up on Roblox’s roadmap although it is a small thing, but still important for rapid code testing.
put this in a plug-in, it should refresh the module whenever you close or deselect it. Test it first on something unimportant. Since this will likely close the editor whenever you deselect the script, it might be more annoying than it is worth. But we can also make it a button or a hotkey.
local ss = game:GetService("StudioService")
local oldVal = ss.ActiveScript
ss:GetPropertyChangedSignal("ActiveScript"):Connect(function()
if oldVal:IsA("ModuleScript") then
oldVal:Clone().Parent=oldVal.Parent
oldVal:Destroy()
end
oldVal=ss.ActiveScript
end)