Scripting Helpers is winding down operations and is now read-only. More info→
Ad
Log in to vote
0

How to use require to use scripts?

Asked by 4 years ago

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

Log in to vote
0
Answered by 4 years ago

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

0
actually the colon(:) and the period(.) matter when you approach it in OOP manner User#23252 26 — 4y
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. killerbrenden 1537 — 4y
Ad

Answer this question