Sign In Register

How can we help you today?

Start a new topic
Answered

Checking if user is newly created

I would like to set some script data values when a new user is created. I thought to do this by seeing if private data contains the key Initialized or something like that when a DeviceRegistrationRequest happens in cloud code, but I get an error that this cannot be done because private data is null. I suppose I could make sure and set a different key in private data before hand no matter what but that seems rather hacky. What is the proper way to go about checking if the player is newly created?


Best Answer

Okay got it working, thanks. I think you meant 

if(Spark.getPlayer().getScriptData("Registered")=== null)

 though


Hi James,


The way to do this would be in the AuthenticationResponse. In the Cloud Code you can check if the user has a key called "Registered", if they don't it must be a new user and set the scriptData "Registered" to true. 


Then if a user who logs in checks if Registered is there they must be a returning user.


  

if(Spark.getScriptData("Registered")=== null) {
    //Do the new user stuff
    Spark.setScriptData("Registered", true);
}

if(Spark.getScriptData("Registered"=== true)){
    //Do returning user stuff
}

  

I would like to note that if you are using Unity you wont be able to access any data stored in Private Data, this information is only usable in Cloud Code. To use information in Unity please use ScriptData.


Shane

Answer

Okay got it working, thanks. I think you meant 

if(Spark.getPlayer().getScriptData("Registered")=== null)

 though

Yes, you are correct, sorry about that! Glad to hear that it's working


Shane

On AuthenticationResponse cloud code


if(Spark.getData().newPlayer === true) {

    //Do the new user stuff

} else {

    //Do returning user stuff

}


1 person likes this
Login to post a comment