Search Unity

  1. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

Question update economy balances of selected players using player id

Discussion in 'Economy' started by sagargiri11, Sep 29, 2023.

  1. sagargiri11

    sagargiri11

    Joined:
    Mar 4, 2017
    Posts:
    3
    From admin point of view how to update economy balances of selected players using player id?
     
  2. unity_lKj2L45mNspniw

    unity_lKj2L45mNspniw

    Joined:
    Dec 20, 2019
    Posts:
    8
    im here for the same question :)
     
  3. boyplayunity

    boyplayunity

    Joined:
    Nov 8, 2017
    Posts:
    7
    through cloud code?
     
  4. unity_lKj2L45mNspniw

    unity_lKj2L45mNspniw

    Joined:
    Dec 20, 2019
    Posts:
    8
    yes through cloud code. I just found out today that we can't update economy now, maybe it'll be on the next update.
     
  5. boyplayunity

    boyplayunity

    Joined:
    Nov 8, 2017
    Posts:
    7
  6. unity_lKj2L45mNspniw

    unity_lKj2L45mNspniw

    Joined:
    Dec 20, 2019
    Posts:
    8
  7. boyplayunity

    boyplayunity

    Joined:
    Nov 8, 2017
    Posts:
    7
    Code (JavaScript):
    1. const _ = require("lodash-4.17");
    2. const { CurrenciesApi } = require("@unity-services/economy-2.4");
    3.  
    4. module.exports = async ({ params, context, logger }) => {
    5.   // Log an info message with the parameters provided to the script and the invocation context
    6.   const {amount} = params;
    7.   const { CurrenciesApi } = require("@unity-services/economy-2.4");
    8.   const { projectId, playerId, accessToken} = context;
    9.   const currencyApi = new CurrenciesApi({ accessToken });
    10.   const currencyId = "TEST1";
    11.   const balance = amount;
    12.   const currencyBalanceRequest = { currencyId, balance };
    13.   const request = { projectId, playerId, currencyId, currencyBalanceRequest };
    14.   const response = await currencyApi.setPlayerCurrencyBalance(request);
    15.  
    16.   return {
    17.     amount : amount + 1
    18.   };
    19. };
    20.  
    I tried this on cloud code web, it worked.