How to replace "Script" with "ModuleScript"?

Question, I have a script that is in a part, and when the game is played, it moves to workspace. I’m trying to figure out how to do it with a ModuleScript. I changed the “script” to “ModuleScript” “MainModule” and everything, but I can’t seem to figure it out. Here’s the line of code.

script.Parent = game.Workspace

where is your module script currently located?

Inside a part, that would be inside another part, and inside a model.

I believe a module script doesnt run code until it is ‘required’

1 Like

Question: What would I write then?

you would write
require(game.Workspace.ModuleScript), I believe, tho this is for using functions that are stored in a modulescript
correct me if i wrong

For example,

require(workspace.ModuleScript)

But why you want to change script to modulescript?

The name, or the ID in library?

I don’t I just want to make a modulescript move to workspace.

its wrong, you can’t pass string in argument, but you can pass ModuleScript.

Why then you dont change code to

require(script.Parent.ModuleScript)

or something

So replace the entire code with that?

Read ModuleScript | Documentation - Roblox Creator Hub, You don’t understand ModuleScript.

I’m just a little stuck, I’m asking how do I move a modulescript thats inside of a part, or model, into workspace once the script is ran.

Create a script and make it do the stuff

Example

local Module = workspace.Part.MainModule
Module.Parent = workspace
1 Like

You need to put this line in a script for the module script to actually run

require(assetIdContainingModuleScript or path.to.your.modulescript)

You also need to make sure that the module script returns something, so put this in the last line of the module script

return true
1 Like

As eveyone else has said, module script code does not run until you require it.

Here is what would work:

-- Module Script - located in game.ServerScriptService
script.Parent = game.Workspace

return true
-- Server Script - Doesn't matter the location as long as it's not somewhere like ServerStorage
require(game.ServerScriptService.ModuleScript)

Basically, you will need TWO scripts to do what you want. Check out Roblox’s tutorial on how to utilize Module Scripts. I think you will benefit from that: https://education.roblox.com/en-us/resources/intro-to-module-scripts

Why are you trying to convert a script into a module script? Main purpose of module scripts is to be used when needed. Not to be moved around. Module scripts is usually located in ReplicatedStorage where programmers can require them and use the module script’s functions.

Module scripts are basically functions but it has more ‘features’. I dont really see the purpose of moving module scripts around