Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
✓ ANSWER ACCEPTED

How to use require to use scripts?

i made a model with a module script in it it says the following:

return function()
	script.MyScript.Parent=game:GetService("ServerScriptService")
	script:Destroy()
end

and i put the into the module script and i create a model but im having a difficultyto run the script like i put a script in ServerScriptService but i don't know how to make it so it will put MyScript into the ServerScriptService

1 answer

✓ Accepted answer

In the Module Script:

local module = {}

function module.WhatEverYouWant(Arguments)
	print("Hello World")
end

function module.WhatEverYouWantAlso(Args)
	print(Args.Name.." Has Left The Game")
end

return module

Or You Can Do This As Well

local module = {}

function module:WhatEverYouWant(Args)
	print(Args.Name.." Has Joined The Game")
end

function module:WhatEverYouWantAlso(Args)
	print(Args.Name.." Has Left The Game")
end

return module

The only difference is on the module function where I changed the period ( . ) to a colon ( : ), it doesn't really matter which on you use.

But to require it, I'd recommend putting it in ReplicatedStorage if you are using a local script or ServerScriptService inside of the script that you are using.

But to require the module script you would do this:

local module = require(script.ModuleScript) --// Just change the "script.ModuleScript" to the location of your module script

And then to run the code inside of the module you would do this:

game.Players.PlayerAdded:Connect(function(player)
	module:WhatEverYouWant(player) --// Or module.WhatEverYouWant(player)
end

game.Players.PlayerRemoving:Connect(function(player)
	module:WhatEverYouWantAlso(player) --// Or module.WhatEverYouWantAlso(player)
end

Hope this helped!

If you have any questions comment below, or use the Roblox Wiki and/or Youtube videos!

Link To Wiki: https://developer.roblox.com/en-us/api-reference/class/ModuleScript

2 comments

0

actually the colon(:) and the period(.) matter when you approach it in OOP manner

0

I'm just talking about in general scripting wise. It matters which programming type you use, but not many people are familiar with OOP, LP, FP, PP, etc. So I was just trying to keep it simple.

This discussion is preserved as it was. New answers, comments, and votes are closed.