Save and Load Objects

In the previous lesson, we stored a number in the Storage. Storage can store booleans, numbers, vectors, colors, tables and player references. If we want to store the hat that the player owns, we can’t just store the reference to an instance of the object. We must store the MUID of the template, a string that is unique for each template. You can find the template MUID by right-clicking in the Project Content and then pressing Copy MUID. You will get a value with an ID and a name like “DC634DB7D0E457C6:WizardHat”. The id is unique so you will have something different for your project.

DC634DB7D0E457C6:WizardHat

Save objects

As the equipment might already be removed for the player when he wants to leave, we will save the fact that the player owns the hat when they buy it.

The function SaveHatInStorage is a function placed inside the ShopServer script that will add a field hat inside the storage of that player. The value is HAT_REF, the MUID of the template. This reference is going to be used to spawn the hat when the player joins the game.

function SaveHatInStorage(player)
    local storage = Storage.GetPlayerData(player)
    storage.hat = HAT_REF
    Storage.SetPlayerData(player, storage)
endCode language: Lua (lua)

That function is called after spawning and equipping the hat.

function OnBuyHat(player)
    if player:GetResource("Coins") < 3 then
        print("Error, not enough coins")
        return
    end	
    player:RemoveResource("Coins", 3)
    SpawnAndEquip(player, HAT_REF)
    SaveHatInStorage(player)
    Events.BroadcastToPlayer(player, "ToggleShop", false)
endCode language: Lua (lua)

Full ShopServer script

local HAT_REF = script:GetCustomProperty("Hat")

function SpawnAndEquip(player, ref)
    local equipment = World.SpawnAsset(ref)
    equipment:Equip(player)
end

function SaveHatInStorage(player)
    local storage = Storage.GetPlayerData(player)
    storage.hat = HAT_REF
    Storage.SetPlayerData(player, storage)
end

function OnBuyHat(player)
    if player:GetResource("Coins") < 3 then
        print("Error, not enough coins")
        return
    end
    player:RemoveResource("Coins", 3)
    SpawnAndEquip(player, HAT_REF)
    SaveHatInStorage(player)
    Events.BroadcastToPlayer(player, "ToggleShop", false)
end

Events.ConnectForPlayer("BuyHat", OnBuyHat)Code language: Lua (lua)

Load and Equip Objects

Now that the hat is stored in the storage, we can copy our function SpawnAndEquip from the ShopServer and paste it into the PlayerStorage script. When the players connect, we check at line 12 if the hat field exists. If it exists, we call the SpawnAndEquip with the player and the MUID of the template.

function SpawnAndEquip(player, ref)
    local equipment = World.SpawnAsset(ref)
    equipment:Equip(player)
end

function OnPlayerJoined(player)
    local storage = Storage.GetPlayerData(player)
    if storage.coins then
        player:SetResource("Coins", storage.coins)
    end
    if storage.hat then
        SpawnAndEquip(player, storage.hat)
    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)

If you open the file inside Temp/Storage in your project files (File > Show Project in Explorer), you will see the two entries:

  • The first one called “coins” with the value 3
  • The second one called “hat” with the MUID of the WizardHat
pbt file inside the Temp/Storage folder of my project

Lesson Content
1 Comment
Collapse Comments

There’s an error for the Full ShopServer script on line 10:

What it looks like:
torage.hat = HAT_REF

What it should look like (just missing the small “s” at the beginning of the line):
storage.hat = HAT_REF

Leave a Comment

Scroll to Top