Script

Show Deprecated

A Script is a type of Lua code container that will run its contents on the server. By default, Scripts have print("Hello, world") as their contents. The instant that the following conditions are met, a Script's Lua code is run in a new thread:

The Script will continue to run until the above conditions are not met, it terminates or it raises an error (unless that error is raised by a function connected to some event that is firing). Additionally, the thread will be stopped if the Script or one of its ancestors is destroyed. A script will continue to run even if the Parent property is set to nil (and the Script is not destroyed).

It has access to server-side objects, properties and events. For example, Scripts can award badges to players using BadgeService, while a LocalScript on the client cannot. Actions taken by LocalScripts that are not replicated (due to Workspace.FilteringEnabled) will not be visible to Scripts.

Code Samples

Hello World

print("Hello, world")

Summary

Properties

The code to be executed.

Methods


Returns a hash of the script's source.

Events

Properties

Source

Plugin Security
Plugin Security

A script's Source is the code to be executed. Modifying the code within a script modifies the source code executes when the script runs.

For instance, given a script containing the line:


print("Hello world!")

The script's source is the "print("Hello world")" command because it is what will be executed when the script runs, leading to "Hello world" being printed in the command line.

This item is protected. Attempting to use it in a Script or LocalScript will cause an error.

If you want to read or modify a script that the user has open, consider using the ScriptEditorService to interact with the Script Editor instead.

Code Samples

Viewing a Script's Source

-- Script named HelloScript in workspace
local function printHello()
print("Hello world!")
end
printHello()
-- Command line
local targetScript = workspace.HelloScript
print(targetScript.Source)

Methods

GetHash

Local User Security

The GetHash function returns a hash of the script's source.

What is a Hash

A hash is a string of characters produced by a cryptographic algorithm that maps data of arbitrary size, since scripts can be short or long, to data of a fixed size, a string of a fixed length. Although they have several uses, hashes are often used to determine the integrity of data and make it faster to look up data. If you compare two pieces of data and their hashes do not match, then the data has changed.

Hashes have three key properties:

  1. They are often of fixed lengths. Since the function uses a hashing algorithm known as MD5, this function always generates hashes consisting of 32 hexadecimal (number and letter) characters.
  2. The same data (e.g. script source) always generates the same hash.
  3. Different data (e.g. different script sources) generate unique hashes. It is highly unlikely that two different sources will generate the same hash.

Limitations

The hash function has several limitations, including:

  • If the script's LinkedSource property is set, then this method will return the empty string.
  • This item is protected. Attempting to use it in a Script or LocalScript will cause an error.

Returns

The 32 character hash value generated by the hash function.

Code Samples

Hashing a Script's Source

-- Script named HelloScript in workspace
local function printHello()
print("Hello world!")
end
printHello()
-- Command line
local targetScript = game.Workspace.HelloScript
print(targetScript:GetHash())

Events