Create NFTAxeLoaderServer Script

The NFT Axe template needs a script that will load a specific NFT. This could load the player’s last selected Axe from storage. In this case, we will set up some custom properties on the script that we can read and send to the API.

Create NFTAxeLoaderServer Script

Create a new script called NFTAxeLoaderServer and place it inside the NFT Axe template that is in the Hierarchy.

  1. Add the NFTAxeLoader API as a custom property called NFTAxeLoader.
  2. Add the AxeGeo group as a custom property called LoadingGeo.
  3. Create a custom property called ContractAddress which is a string type.
  4. Create a custom property called TokenID which is a string type.

Set ContractAddress Property

All lazy minted tokens on Core will have the same contract address. This can be an issue because it means loading a collection is not possible. You would need to store all the token IDs locally in a data table. In this case, we are after loading a specific token for an Axe the player may have equipped.

Set the ContractAddress property to 0x495f947276749ce646f68ac8c248420045cb7b5e.

Set the Token ID Property

The TokenID property is the ID of the NFT you will be loading. You could imagine an inventory of axes that the player has purchased, and they could select one to load instead. For this case, we will keep it simple and load a specific NFT for this Axe.

There are a couple of ways you can get the Token ID for an NFT. The easiest way on OpenSea is either from the URL when you access the NFT or from the details section on the page when looking at the NFT. Once you have the Token ID, update the TokenID property with the ID.

Create Template

The NFT Axe in the Hierarchy is finished. No more changes need to be done, so this can be created as a template. Right click on the NFT Axe, and select New Template From Object.

Edit NFTAxeLoaderServer Script

Open up the NFTAxeLoaderServer script and add the variables for the custom properties that were added so you have a reference to them.

local NFTAxeLoader= require(script:GetCustomProperty("NFTAxeLoader")) local LOADING_GEO = script:GetCustomProperty("LoadingGeo"):WaitForObject() local TOKEN_ID = script:GetCustomProperty("TokenID") local CONTRACT_ADDRESS = script:GetCustomProperty("ContractAddress")
Code language: Lua (lua)

Connect EquippedEvent

When the NFT Axe is equipped by the player, the equippedEvent is fired. We can use this to call a function from the NFTAxeLoader API that will then load the NFT based on the arguments passed in.

script:FindAncestorByType("Equipment").equippedEvent:Connect(function(equipment, player) NFTAxeLoader.load(player, TOKEN_ID, CONTRACT_ADDRESS, LOADING_GEO) end)
Code language: Lua (lua)

The NFTAxeLoaderServer Script

NFTAxeLoaderServer

local NFTAxeLoader = require(script:GetCustomProperty("NFTAxeLoader")) local LOADING_GEO = script:GetCustomProperty("LoadingGeo"):WaitForObject() local TOKEN_ID = script:GetCustomProperty("TokenID") local CONTRACT_ADDRESS = script:GetCustomProperty("ContractAddress") script:FindAncestorByType("Equipment").equippedEvent:Connect(function(equipment, player) NFTAxeLoader.load(player, TOKEN_ID, CONTRACT_ADDRESS, LOADING_GEO) end)
Code language: Lua (lua)

Test the Game

Test the game by playing it and picking up the NFT Axe. After a little while, the parts for the blade and handle will spawn in. While the Axe is loading, the player can see that, but can also still use the Axe. So it won’t slow down the player’s experience.

Scroll to Top