Document stub.png This article or section may not have content matching Scratch Wiki editing standards. Please improve it according to Scratch Wiki:Guidelines and Scratch Wiki:Editing Conventions. (June 2018)
Reason: This article contains inconcise language.

Multiplayer games are games in which more than one person can play at once at the same time, often from different computers. Multiplayer games can be made and played on the same computer (A local multiplayer) or with different computers from different places through the use of cloud variables.

Note Note: This no longer works, due to the 128-character limit that was imposed on Cloud Variables in March 2018.

Local Multiplayer

Since local multiplayer games are played on the same computer for several players, they don't use cloud variables. Local multiplayer games are based off the following scripts:

A script is made for the first player:

when green flag clicked
forever
if <key [left arrow v] pressed?> then
move(-10) steps
end
if <key [right arrow v] pressed?> then
move (10) steps
end

A script for the second player is made. Notice the different keys both players press to move up and down.

when green flag clicked
forever
if <key [a v] pressed?> then
move(-10) steps
end
if <key [d v] pressed?> then
move (10) steps
end

The scripts can be programmed differently depending on what is needed for the project. When creating a local multiplayer game, one player usually has one side of the keyboard, such as the number keys or arrow keys, and the other player has the other side of the keyboard, such as the W, A, S, D, and/or F keys. This makes the game easier to play. However, a downside is that on some laptop computers, a keyboard may only input a certain number of keys before reaching a maximium and not remembering newer keys being pressed.

Online Multiplayer

Online multiplayer games can be made with cloud data. There are many ways to make an online multiplayer game. In all of these examples, however, you must attain full scratcher status. New Scratchers can't do this because of the restrictions placed upon Cloud Variables.

Realistic online multiplayer games or MMO (such as Cloud Platformer Multiplayer Fun by griffpatch) are scarce due to cloud data limits and non-existence of cloud lists. However this tutorial https://scratch.mit.edu/projects/77775784/ , and the one below simplify the basics of a multiplayer.

Variables

First off cloud variables need to be created to help with movement and detecting if a player is still active.

These variables should be created:

Note Note: It is possible to use less variables by using a variable for 2 purposes, but is more complex.
(☁ cloud check)//checks to see if the two players are connected. set to 1.
(☁ Player1 check)//checks if player is still on
(☁ Player2 check)//same with this one
(☁Player1 coords)//sends coordinates
(☁Player2 coords)//again here
(local player1 check)//compare with the cloud variable
(local player2 check)//ditto
(Player ID)//just telling which sprite you control

Coding

Once those variables are made, it is needed to check if anyone else is on the project and to connect the player to the cloud with a slot if needed.


Backdrop
Note Note: The connection test does not work if you are not signed in, disconnected from the Internet, or a New Scratcher. However, it can still see cloud data.
when green flag clicked
broadcast [start v]//just have it so that there is only 1 green flag clicked
wait (2) secs//make the project is the connected to the cloud
set [☁cloud check v] to [0]//If the player is a new scratcher or offline this should not work
set [☁cloud check v] to [1]//Then this won't work either
if<(☁ cloud check)=[1]> then//if it equals 1, they are connected, if not, they are not
set [local player1 check v] to (☁player1 check)
set [local player2 check v] to (☁player2 check)
wait (5) secs//waiting because if a player is active, the player1 and 2 cloud variables will change every second so it is needed to make sure  by waiting 5
if <(local player1 check)=(☁player1 check)> then//if the cloud check has not changed it means that nobody is playing as player 1, therefore, the player has been set as player1
set [player ID v] to [1]//not cloud because the cloud variable for player1 will be updated so that other computers know that there is a player1
set [☁player1 check v] to [0]//just so it does not get that variable up to a million eventually
set [☁player1 coords v] to[500500]//the coordinate system we use adds 500 to each coordinate because then every number is a positive 3 digit number and we do not need an encoder or decoder, basically, that is 0,0
broadcast [joined v]//tell the other sprites to do their stuff
else
if<(local player2 check)=(☁player2 check)> then//doing the same thing as above but if there already is a player1, it is needed to check for a player2
set [player ID v] to [2]
set [☁player2 check v] to [0]
set [☁player2 coords v] to[500500]
broadcast [joined v]
else
broadcast [full v]//if both slots are taken a sprite is needed to tell the player the game is full
end
end
else
broadcast [not connected v]//if the cloud test we ran at the beginning fails, it is needed to tell them they are not connected to the cloud or are a new scratcher
end

We add this script for Player1's sprite.

when I receive [joined v]
wait (1) secs//just so that the sprite that tells you stuff has time
if <(playerID)=[1]> then
forever//just the movement script
if <key [right arrow v] pressed> then// This code is for the movement of Player1.
change x by (3)
end
if <key [left arrow v] pressed> then
change x by (-3)
end
if <key [down arrow v] pressed> then
change y by (-3)
end
if <key [up arrow v] pressed> then
change y by (3)
end
set [☁player1 coords v] to (join((x position)+(500))((y position)+(500)))// telling all the other computers player1's position with the plus 500 method mentioned before
end
else
forever//if the player ID isn't one the sprite of player1 will go to the coordinates, again using the 500 method, but this time it is needed to subtract because these are the ones we receive from the other computer so they have already had 500 added
go to x:((join (letter (1) of (☁player1 coords))(join (letter (2) of (☁player1 coords))(letter (3) of (☁player 1 coords))))-[500])y:((join (letter (4) of (☁player1 coords))(join (letter (5) of (☁player1 coords))(letter (6) of (☁player 1 coords))))-[500])
end
end
when I receive [joined v]
if <(playerID)=[1]> then
forever
change [☁player1 check v] by (1)//at the beginning when the program is first run there is a five second wait for this variable to change, because then it is recorded whether there is a player1 or not
wait (1) secs
end

This script is for Player2's sprite. All of player1's variables are transferred to player2.

when I receive [joined v]
wait (1) secs
if <(playerID)=[2]> then
forever
if <key [right arrow v] pressed> then
change x by (3)
end
if <key [left arrow v] pressed> then
change x by (-3)
end
if <key [down arrow v] pressed> then
change y by (-3)
end
if <key [up arrow v] pressed> then
change y by (-3)
end
set [☁player2 coords v] to (join((x position)+(500))((y position)+(500)))
end
else
forever
go to x:((join (letter (1) of (☁player2 coords))(join (letter (2) of (☁player2 coords))(letter (3) of (☁player 2 coords))))-[500])y:((join (letter (4) of (☁player2 coords))(join (letter (5) of (☁player2 coords))(letter (6) of (☁player 2 coords))))-[500])
end
end
when I receive [joined v]
if <(playerID)=[2]> then
forever
change [☁player2 check v] by (1)
wait (1) secs
end

This script is for telling players of errors (such as an error connecting).

when gf clicked
go to x:(0) y:(0)//make it center and so we can see it
go to front
show
switch costume to [connecting v]//just a costume that says "connecting"
when I receive [full v]
switch costume to [full v]//says something like "sorry, there are no open slots, please try again later"
when I receive [not connected v]
switch costume to [not connected v]//it will say "sorry you are either a new scratcher or are not connected to the cloud"
when I receive [joined v]
if<<<(playerID)=[1]>and<(☁player2 check)=(local player2 check)>>or<<(playerID)=[2]>and<(☁player1 check)=(local player1 check)>>> then//if the other player wasn't active
switch costume to [no one else on v]//says "slot found and connected but no one else is online"
else
switch costume to [someone else found v]//says slot "found and connected someone else is online"
end
wait (1) secs//The player(s) will have time to read this message
hide//The gameplay will start

See Also