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

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

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

✓ Accepted answer

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

local plr = game.Players.LocalPlayer

script.Parent.MouseButton1Click:connect(function()
	plr.Val.Value = plr.Val.Value + 1
end)

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

game.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:

local library = require(script.Parent.ModuleScript)

for _,v in pairs(library) do
	print(v.." ")
end

Module Script:

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

return 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

1 comment

0

This made the most sense, but thank you for your efforts.

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..

function brick()

local brick=game.Workspace.brick

end

function brick2()

brick.Transparency=1

end

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...

function brick()

brick=game.Workspace.brick

end

function brick2()

brick.Transparency=1

end

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.

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

local 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:

game.workspace.Part:destroy()

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

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