How to run code in a module script

hello I am trying to run code in a module script without calling anything:

local module = {}

print("hello world") -- never runs

return module

as you can see, it does not run. How do I get it to run without having to call it?

Why do this?

I want to run code alongside module functions that can be called. The module functions can affect the behavior of the code.

1 Like

You can’t. ModuleScripts do not run unless called

Edit: What do you mean by ‘called’?

1 Like

you gave a solution that used a word you didnt know? how do you not know what called means as a programmer

2 Likes
local module = {}

function module:run()
print("hello world")
end

return module
script

local module = require(modulelocation)
module:run()
2 Likes

The module script runs client sided code only. I guess I could call it from the local script but its stupid localscripts dont work in workspace because I want to organize my modules rather than having junk everywehre

2 Likes

then put it in a local script…

char limit

2 Likes

Here’s an example:

return function()
	print("hello world")
	local module = {}
	return module
end
1 Like

what does your module even do?

1 Like

I know what ‘called’ means. I was asking what you meant in the context of the question since you mentioned functions.


Anyway, were you asking if you can run code within a ModuleScript without calling a specific function in the ModuleScript? If so, just require a ModuleScript normally; there’s nothing additional you need to do

1 Like

I want everything to be inside of the modules folder inside workspace and local scripts dont run in workspace but this is a client sided module

if I put a local script to call the module, it wont run in workspace.

1 Like

then put the local script inside where it does run, StarterGui works

2 Likes

this is messy and unorganized tho. Perhaps I will make a script that unloads a local script into starterGUI

1 Like

Module scripts I’m pretty sure can’t run without being required
You’re best bet is something like this:

return function()
	print("hello world")
	local module = {}
	return module
end
1 Like

You can create a Script and set its RunContext to Client to run a LocalScript in workspace

1 Like

dude what are you even trying to do i really cant help you if you dont give any context to what youre trying to do

1 Like

You’re not helping, stop asking for context and solve the actual problem

1 Like

setting the runcontext to client literally just chanegs it to a local script which dont run in workspace lol

1 Like

he said it doesnt run in workspace told him put it in StarterGui and says thats messy…

1 Like

Problem: Want to run code in modulescript
Solution: Ask for context instead of trying to solve the problem

1 Like

I don’t blame him. This is looking like an XY problem.

It’s such a redundant use case — it goes against the very principle of a ModuleScript.

1 Like