Client-Side Scripting and Server-Side Scripting: Difference between revisions
>MrHistory |
Defining some best practices for old ROBLOX |
||
(27 intermediate revisions by 7 users not shown) | |||
Line 1: | Line 1: | ||
The '''server''' can be thought of as the version of the game running on the computers at Roblox. Its job is to keep track of everything, and make sure that all the clients are synchronized. Server-side scripts run only on the computer at Roblox. | |||
==Servers== | The '''client''' is the software that you and other players run on the computer in order to play the game. Client-side scripts run on every player's computer. | ||
[[Image: | == Servers == | ||
[[Image:Servers.JPG|200px|thumb|List of servers]] | |||
Servers hold the data that the clients need. Using servers, data can be transferred from one client to the all the others. Server-side scripting affects the server. The server sends data to the clients, so if the data is affected, so are the clients. Parts are groups of data, so if a server-side script adds a part to the server, the part will be visible to all the clients. Most scripts are server-side so players can interact with each other; it is an online hangout. To create server-side scripts, you must insert the object "Script." Server-side scripts do not run in StarterGui or StarterGear. This can be used as an advantage, so that the scripts will only run once it is copied into a new player's Backpack or PlayerGui. You have probably been using mainly server-side scripts in your scripting journey so far. Let's create a simple server script. | |||
{{Example| | |||
# Go to Studio and create a new document. (Ctrl+N) | |||
# Create a baseplate large enough for players to walk on. (Remember to anchor it.) | |||
# In the toolbar, go to Insert>Object>Script. | |||
# Open up the script. | |||
# In the script, create a Part and make it change colors like so: | |||
<syntaxhighlight lang="lua" line> | |||
local part = Instance.new("Part", nil) | |||
part.Anchored = true | |||
part.Position = Vector3.new(0, 5, 0) | |||
part.Parent = workspace | |||
while true do | |||
part.BrickColor = BrickColor.Random() | |||
wait(3) | |||
end | |||
</syntaxhighlight> | |||
# Now that you have your script created, start a server. Go to Tools>Test>Start Server. | |||
# A new window should open up. This is the server. Now, players must be added to the server. | |||
# From the server window, go to Tools>Test>Start Server. | |||
# Another window should open up. This window is the view of the player that was created. | |||
}} | |||
== Best practices == | |||
These are some best practices for clients in [[Help:Contributing|the scope of this wiki]]. | |||
* [[Tool]] code that needs to read the [[Mouse]] properties or connect to [[Mouse]] events should be in [[LocalScript]]s. Any other [[Tool]] code should be in [[Script]]s. | |||
** [[Script]]s may connect to [[Tool]] [[Activated]] instead of connecting to [[Mouse]] [[Button1Down]]. When a [[Player]] clicks somewhere, the [[Humanoid]] [[TargetPoint]] will update on the server. | |||
* Put code for User GUIs in [[LocalScript]]s. | |||
** For example, have a [[LocalScript]] in the User GUI connect to a [[StringValue]] [[Changed]] to display a global message instead of having a [[Script]] in [[Workspace]] control the User GUI directly. | |||
[[Category:Scripting Tutorials]] |
Latest revision as of 17:08, 9 June 2023
The server can be thought of as the version of the game running on the computers at Roblox. Its job is to keep track of everything, and make sure that all the clients are synchronized. Server-side scripts run only on the computer at Roblox.
The client is the software that you and other players run on the computer in order to play the game. Client-side scripts run on every player's computer.
Servers
Servers hold the data that the clients need. Using servers, data can be transferred from one client to the all the others. Server-side scripting affects the server. The server sends data to the clients, so if the data is affected, so are the clients. Parts are groups of data, so if a server-side script adds a part to the server, the part will be visible to all the clients. Most scripts are server-side so players can interact with each other; it is an online hangout. To create server-side scripts, you must insert the object "Script." Server-side scripts do not run in StarterGui or StarterGear. This can be used as an advantage, so that the scripts will only run once it is copied into a new player's Backpack or PlayerGui. You have probably been using mainly server-side scripts in your scripting journey so far. Let's create a simple server script.
- Go to Studio and create a new document. (Ctrl+N)
- Create a baseplate large enough for players to walk on. (Remember to anchor it.)
- In the toolbar, go to Insert>Object>Script.
- Open up the script.
- In the script, create a Part and make it change colors like so:
local part = Instance.new("Part", nil)
part.Anchored = true
part.Position = Vector3.new(0, 5, 0)
part.Parent = workspace
while true do
part.BrickColor = BrickColor.Random()
wait(3)
end
- Now that you have your script created, start a server. Go to Tools>Test>Start Server.
- A new window should open up. This is the server. Now, players must be added to the server.
- From the server window, go to Tools>Test>Start Server.
- Another window should open up. This window is the view of the player that was created.
Best practices
These are some best practices for clients in the scope of this wiki.
- Tool code that needs to read the Mouse properties or connect to Mouse events should be in LocalScripts. Any other Tool code should be in Scripts.
- Scripts may connect to Tool Activated instead of connecting to Mouse Button1Down. When a Player clicks somewhere, the Humanoid TargetPoint will update on the server.
- Put code for User GUIs in LocalScripts.
- For example, have a LocalScript in the User GUI connect to a StringValue Changed to display a global message instead of having a Script in Workspace control the User GUI directly.