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.
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.
functionOnPlayerJoined(player)print(player.name.." just joined.")
endfunctionOnPlayerLeft(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:
Get the current storage table using the Storage.GetPlayerData function.
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”.
The last line is updating the storage with the new table containing the number of coins.
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:
Get the current storage table using the Storage.GetPlayerData function.
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.
functionOnPlayerJoined(player)local storage = Storage.GetPlayerData(player)
if storage.coins then
player:SetResource("Coins", storage.coins)
endendCode 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:
functionOnPlayerJoined(player)local storage = Storage.GetPlayerData(player)
if storage.coins then
player:SetResource("Coins", storage.coins)
endendfunctionOnPlayerLeft(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)
This website uses cookies so that we can provide you with the best user experience possible. Cookie information is stored in your browser and performs functions such as recognising you when you return to our website and helping our team to understand which sections of the website you find most interesting and useful.
Strictly Necessary Cookies
Strictly Necessary Cookie should be enabled at all times so that we can save your preferences for cookie settings.
If you disable this cookie, we will not be able to save your preferences. This means that every time you visit this website you will need to enable or disable cookies again.
3rd Party Cookies
This website uses Google Analytics to collect anonymous information such as the number of visitors to the site, and the most popular pages.
Keeping this cookie enabled helps us to improve our website.
Please enable Strictly Necessary Cookies first so that we can save your preferences!