Command Bar Problem (with ModuleScripts)

yeah it doesn’t print something

Can you please show us how you have explorer setup?

Also, can you please try renaming the script to something much simpler (e.g. just ForCommands), and calling it with the new name

yeah i will try that thanks (ignore this)

Any luck?

Code and command look fine. While the style @SeargentAUS suggested is more standard, there’s nothing wrong with returning an anonymous table in the manner the original screenshot shows. When defining key, value pairs in a table ["Disable"] = ... is equivalent to Disable = ... (This is true from the other perspective as well: module["SomeKey"] is equivalent to module.SomeKey so both could be used interchangeably depending on need or style).

Your issue may actually be stemming from the caching behavior of require calls. When you require a ModuleScript in a particular Lua environment, the value returned from that call is cached so that subsequent require calls on the same ModuleScript return the same value. The command bar has its own Lua environment so if you require a ModuleScript and then make changes to its source code, the value returned from the first call will still persist. This applies regardless of it you use a variable to cache the result from the first require call or if you require the ModuleScript a second time after the edit (both will be referencing the value returned from the first require call).

In order to “refresh” the source code of a ModuleScript for a subsequent require call from the command bar, you have to cut out the ModuleScript and paste it back in (or at least this is the best solution I’ve found).

Example

Create a ModuleScript in ReplicatedStorage with the following code:

return {
   Run = function() print("First run") end)
}

Now run the following line in the command bar:

require(game.ReplicatedStorage.ModuleScript).Run()
--Output: First run

Edit the ModuleScript’s code to the following (without cutting and pasting)

return {
   Run = function() print("Second run") end)
}

Run the exact same command

require(game.ReplicatedStorage.ModuleScript).Run()
--Output: First run

The output remains the same since the second require call returned the cached state of the ModuleScript’s code. In order for the edits made to the ModuleScript to apply the cached Lua environment of the command bar needs to be cleared, which can be done by saving and closing studio OR you can cut and paste the ModuleScript (I’m not 100% sure why this works - I believe it may be because studio no longer recognizes the ModuleScript as the same one as before). Hacky, yes. But helpful for testing changes to ModuleScript required from the command bar in studio.

1 Like

Thanks for this really helpful i hope roblox will fix this issue cuz its so annoying, copy and pasting the scripts anyways thanks :slight_smile:

An alternative you could do is to set out the script like this

function Disable()

end

function Enable()

end

then, instead of using require, use loadstring:

loadstring(game.ReplicatedStorage.ForCommands.Source) 

Ensure you are accessing the SOURCE of the code for this method

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.