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

Explain to me the difference between Scripts, LocalScripts, ModuleScripts, and ServerScripts?

Asked by
OniiCh_n 410 Moderation Voter
11 years ago

The wiki doesn't do a good job explaining the difference between these objects. I mostly have trouble discerning the usage between Scripts and LocalScripts. What is the difference?

3 answers

Log in to vote
4
Answered by 11 years ago

A local script runs on your machine so it would be more to do with things like GUIs

1local plr = game.Players.LocalPlayer
2 
3script.Parent.MouseButton1Click:connect(function()
4    plr.Val.Value = plr.Val.Value + 1
5end)

While a script (which is I believe the same thing as a server script) runs.. well.. on the server! An example:

1game.Workspace.Part:remove()

Also module scripts are in a way like libraries, they would be used as a way to sort of make your code look all pretty and short and non repetitive if you are using it in multiple scripts

Main Script:

1local library = require(script.Parent.ModuleScript)
2 
3for _,v in pairs(library) do
4    print(v.." ")
5end

Module Script:

1local lib = {1,2,3,4,5,6,7,8,9}
2 
3return lib

OUTPUT => > 1 2 3 4 5 6 7 8 9

Note: Module scripts have to have a table that is returned. which is why lib is there

0
This made the most sense, but thank you for your efforts. OniiCh_n 410 — 11y
Ad
Log in to vote
3
Answered by
haillin 60
11 years ago

I can tell you what two of them are pretty well.

So basically in LUA there is something called Lexical Scoping. so the best way to show you would be through example..

01function brick()
02 
03local brick=game.Workspace.brick
04 
05end
06 
07function brick2()
08 
09brick.Transparency=1
10 
11end

in the Above example THIS WOULD NOT WORK, because when you declare a variable locally it can only be used within the block that it was created.. those or two different functions so brick is local to the first one... However if you were to this...

01function brick()
02 
03brick=game.Workspace.brick
04 
05end
06 
07function brick2()
08 
09brick.Transparency=1
10 
11end

THIS WOULD WORK. You are now declaring brick as a global variable and it can be used anywhere throughout the script. I know local variables are local to functions and even loops as well, if you were to declare a local function within a loop for example since it was created in the loop it could only be used in the loop.

Now you may be saying, why would I bother to do local variables when I can just do global. The reason for this is because you don't want to fill up the global enviornment with extra uneeded garbage. Local variables run faster and generally it's just proper form to use them whenever possible.

Log in to vote
2
Answered by 11 years ago

Local scripts are Local, meaning they do local things to the local player example:

1local player = game.players.LocalPlayer

That is what a local script might do.

A script is not going to do local stuff like above, it instead does things like remove parts globally example:

1game.workspace.Part:destroy()

I hope that got 1/2 of you're answer but I don't know about the others.

Answer this question