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

Whats the difference between Local Script and a regular Script?

Asked by 4 years ago

The name says most of it. Why would I use a local script instead of a regular and what does it do?

2 answers

Log in to vote
1
Answered by 4 years ago

There's quite a few noticeable differences and they have a big impact on how your game runs.


Local Script

Wiki

A LocalScript is for ClientSided use, so if you want something to happen on the client side, that no one else can see and they won't be affected by it, you would want to use a LocalScript.

LocalScripts will ONLY function if they are a descendant (located inside of):

  • A Player’s Backpack, like a tool.

  • A Player's CharacterModel, so inside of their actual Character that's located in Workspace.

  • A Player's PlayerGui, you would use LocalScripts a lot here since you want guis mainly client sided.

  • A Player's PlayerScripts, this is where code inside of these LocalScripts will happen inside of the player itself.

  • ReplicatedFirst Service, you can disable the Default Loading Screen through LocalScripts if you have your custom LoadingGui.


Server Script

Wiki

A ServerScript is for ServerSided use, so if you want something to happen on the server, where everyone will see it, and it will affect everyone, you would want to use a ServerScript.

ServerScript's will run in a new thread when they are a descendant (located inside of):

  • Workspace

  • ServerScriptService

Anything ran by LocalScripts will not be replicated when FilteringEnabled is enabled, as this is to prevent exploits. To bypass this, you will have to use RemoteEvents. ServerScripts on the other hand, will be replicated since this is ServerSided and not ClientSided, as this will happen on everyone's screen.

ServerScripts DO NOT have access to game.Players.LocalPlayer, you will have to use different functions to retrieve the said player, but LocalScripts do have access to game.Players.LocalPlayer


LocalScript Example

Inside of StarterGui

wait(5)

if game.Players.LocalPlayer.Name == "Player1" then
    local part = Instance.new("Part",workspace)
    part.CFrame = game.Players.LocalPlayer.Character.HumanoidRootPart.CFrame
end

Result


ServerScript Example

Inside of ServerScriptService

game.Players.PlayerAdded:Connect(function(player)
    wait(5)
    local part = Instance.new("Part",workspace)
    part.CFrame = player.Character.HumanoidRootPart.CFrame
end)

Result


Not Really What You Asked For, But I'll Also Explain Module Scripts!


Module Script

Wiki

ModuleScripts are scripts filled with information in short-term words. It holds information and returns one value, and to retrieve it you have to use require and require the module.

ModuleScripts are essential objects for adhering to the don’t-repeat-yourself (DRY) principle.

  • This basically means, it keeps you from writing the same code over and over when you are using it more than once, you can keep it simple using ModuleScripts and just write it there, and use that instead of copying and pasting the same functions. It also keeps your code neat.

You CAN use LocalScripts and ServerScripts to require ModuleScripts.

NOTE: This Is Important!

Note that the first call to require on a ModuleScript will not yield (halt) unless the ModuleScript yields (e.g. calls wait). The current thread that called require will yield until a ModuleScript returns a value. A run time error is generated if this doesn’t happen. If a ModuleScript is attempting to require another ModuleScript that in turn tries to requires it, the thread will hang and never halt (cyclic require calls do not generate errors). Be mindful of your module dependencies in large projects!


You can use ModuleScripts to return certain values, for example.

These are all in ServerScriptService

ModuleScript

local mainModule = {}

function mainModule.GetPlayerName(player)
    local playerName = player.Name
    return playerName
end

return mainModule

You can then set this to a variable and use it. Like this.

ServerScript

local mainModule = require(script.Parent.MainModule)

game.Players.PlayerAdded:Connect(function(player)
    local playerName = mainModule.GetPlayerName(player)
    print(playerName)
end)

Result: prints killerbrenden, the player name.

You can also just use it to do other things, like print things.

ModuleScript

local mainModule = {}

function mainModule:GetPlayerInfo(player)
    print(player.Name)
    print(player.AccountAge)
end

return mainModule --// Important

ServerScript

local mainModule = require(script.Parent.MainModule)

game.Players.PlayerAdded:Connect(function(player)
    mainModule:GetPlayerInfo(player)
end)

Result: killerbrenden & 2680 - Account Age in days

NOTE: You will ALWAYS have to return the module, otherwise it won't work.


Resources


Psst, I was going to explain RemoteEvents and RemoteFunctions but I couldn't! I reached over 15,000 characters and the limit is 10,000!.

Here is a post on RemoteEvents

Hope this helped! If this helped you understand, don't forget to select this as an answer!

Ad
Log in to vote
0
Answered by
Geobloxia 251 Moderation Voter
4 years ago

A local script is a scripts that executes on the client and doesn't execute to the server while a script does. For example, if you use a localscript to make yourself invisible, on your screen you will look invisible. But on other player's screens, you would be visible. If you use a script, you will be invisible on both screens. Scripts are server-side while localscripts are client-side.

Answer this question