Save and Load Resources

In this lesson, we are using Player Storage to save data between sessions. This is important if you want your player to come back in your game. In our current game, we want to keep the Coins and also the equipment if we bought any. This lesson is focusing on the Coins and the next one on the equipment.

Before diving into the scripting part, you must check the Enable Player Storage option in the Properties of the Game Settings object in the Hierarchy.

Game Settings Properties

Player Joined/Left Events

To detect when a player joins or leaves a game, you can connect functions to the Game.playerJoinedEvent and Game.playerLeftEvent. Create a script called PlayerStorage in your scene. This is not inside a Client Context as only the server scripts have access to the Storage. Paste this code and press start.

function OnPlayerJoined(player) print(player.name.." just joined.") end function OnPlayerLeft(player) print(player.name.." just left.") end Game.playerJoinedEvent:Connect(OnPlayerJoined) Game.playerLeftEvent:Connect(OnPlayerLeft)
Code language: Lua (lua)

Save Resources

The functions are now connected to the right event. As those events are also giving us the reference to the player that is joining or leaving, we can get the number of resources for that specific player. To save the resource, we must:

  1. Get the current storage table using the Storage.GetPlayerData function.
  2. We get the number of Coins and we assign it to a field called “coins” inside the storage table. You can add as many fields as you want while the storage is less than 32kB. You can also use the field names that you want, so instead of “coins”, we could call that “money”.
  3. The last line is updating the storage with the new table containing the number of coins.
function OnPlayerLeft(player) local storage = Storage.GetPlayerData(player) storage.coins = player:GetResource("Coins") Storage.SetPlayerData(player, storage) end
Code language: Lua (lua)

Note: you can check the size of your Storage using Storage.SizeOfData(storage). If this value is less than 32000, then it will be saved without losing data.

Load Resources

To retrieve the number of Coins the player had when they disconnected, we have to:

  1. Get the current storage table using the Storage.GetPlayerData function.
  2. Check if the coins field is defined (nil if it is the first session for this player), and set the resource Coins to the number of coins previously saved.
function OnPlayerJoined(player) local storage = Storage.GetPlayerData(player) if storage.coins then player:SetResource("Coins", storage.coins) end end
Code language: Lua (lua)

Hint: You can look at the player storage by opening the Temp/Storage folder inside your project files (File > Show Project in Explorer). This storage is only your local storage for debug purposes and is not linked to the storage of your game once it’s published. Removing that file is the easiest way to check if a new player won’t have any issue with missing fields in his storage.

Here is the final script PlayerStorage:

function OnPlayerJoined(player) local storage = Storage.GetPlayerData(player) if storage.coins then player:SetResource("Coins", storage.coins) end end function OnPlayerLeft(player) local storage = Storage.GetPlayerData(player) storage.coins = player:GetResource("Coins") Storage.SetPlayerData(player, storage) end Game.playerJoinedEvent:Connect(OnPlayerJoined) Game.playerLeftEvent:Connect(OnPlayerLeft)
Code language: Lua (lua)
Scroll to Top