Answered by
6 years ago Edited 6 years ago
Module scripts are mainly used to store functions that are used in multiple scripts in games. Unlike regular scripts, module scripts only return one value that's returned when you require the modulescript.
One example: people use module scripts to store their datastore functions so they can use them in multiple scripts without having to rewrite the function in each script. Example:
08 | function DataStore:AddPlayer(Plr) |
09 | local MyData = Data:GetAsync(Plr.UserId) or { } |
10 | MyData.Ah = MyData.Ah or "Yes" |
12 | PlrData [ Plr.UserId ] = MyData |
15 | function DataStore:RemovePlayer(Plr) |
16 | if PlrData [ Plr.UserId ] then |
17 | Data:SetAsync(Plr.UserId, PlrData [ Plr.UserId ] ) |
18 | PlrData [ Plr.UserId ] = nil |
So then, you can require the module script from another script and use the functions as normal:
01 | local DataStore = require(modulescript) |
03 | for I, V in next , Players:GetPlayers() do |
04 | DataStore:AddPlayer(V) |
07 | Players.PlayerAdded:Connect(DataStore:AddPlayer) |
09 | if not game:GetService( "RunService" ):IsStudio() then |
10 | Players.PlayerRemoving:Connect(DataStore:RemovePlayer) |
Hope this helped. If you have any questions comment below :)