PlayFab API | Basics for Integrating a high end web dashboard to your roblox games!

Resource Page

Hello there! Today I will be showing you how to integrate all you’ll ever need for your roblox games on the web!

Let’s start with the basics. If you go to the module linked in resources. You’ll find a module called PlayFabSettings. That is the very first thing you are going to want to go to in order to begin linking up your PlayFab dashboard.

Next, you will want to create a PlayFab account if you don’t already have one. You can start by going here. PlayFab Website

After you’ve created your account and followed all of the steps. You should see your studio and title on the screen and it’ll look something like this

Notice that on the bottom right of the title that says, “My Game” there is an “ID” on it. This is your title ID and you’ll need it to link up your dashboard. So go to the PlayFabSettings module and under where it says TitleId put the ID of your title there.

Now. in order to finish setting up the module you’ll need to put what is known as a developers secret key

This is a long string that can be found in your titles settings.

Go to your title
At the top left, click on the gear icon and go to Title Settings
You’ll see a list of tabs at the top, you’ll want to go to Secret Keys

You should see a page similar to this.

Now copy the text under the Secret Key and put it into your PlayFab module under devSecretKey

Please note that due to roblox’s difference in use case when it comes to handling these secret keys, you should not need to generate any key other than the one your title is defaulted with

And there! Now you are ready to start integrating PlayFab into roblox!

Due to complexity I will only be going over the Server and a brief idea of Client API in this tutorial. if you would like a tutorial for the others in the future feel free to reach out!

So first things first, we’ll be creating a player login api call using PlayFabServerApi.LoginWithCustomServerId Api

To create an api call, we’ll want to require the PlayFabServerApi and create a function called loginPlayer with the userId as the parameters and insert our call method.

local PlayFabServerApi = require(game.ServerScriptService.PlayFab.PlayFabServerApi)

local function loginPlayer(userId)
    PlayFabServerApi.LoginWithServerCustomId(
        {
            CreateAccount = true; --Set to true if you want it to create a new account if the userid doesn't have an account already.
            ServerCustomId = userId 
        }),
end

This will successfully login your player. Now, while this may login your player. It’s important to note that the PlayFabServerApi has 2 callback functions as the 2nd and 3rd parameter. Those are for on success and on fail. This is extremely important. On Success will be the only way to fetch data that the PlayFab API returned for each callback. This will be important for retrieving any data you may need that the PlayFab API returns.

So to finish off our Login request we’re going to need to add 2 functions to the api call parameters.

local function loginPlayer(userId: number)
    PlayFabServerApi.LoginWithServerCustomId(
        {
            CreateAccount = true;
            ServerCustomId = userId 
        },
        function(result: table)
            print("Successfully logged in: "..userId)    
        end,

        function(error: table)
            print("Failed to login", userId..":", error.errorMessage)
        end)
end 

And just like that, this should work like a charm!

Now what if you want to write data to your title for some analytics? Maybe you want some analytics for when your player left the game and at what point that player left, weather it be gameplay, maybe a tutorial even. Well no have no fear cause with Playfab that is a very simple task!

When a player leaves the game, you’ll want to use PlayFabServerApi.WriteTitleEvent

So we’ll want to create a playerLeft function and start our api call as usual.

        local  playerState = "Tutorial-1"
        local function playerLeft(userId)
            PlayFabServerApi.WriteTitleEvent({
                EventName = "player_left";
                Body = {
                    PlayerId = userId;
                    leaveState = playerState
                }
            })
        end

For the example here I’ve created a variable. You can create your own state machine if you so wish to handle this :slight_smile:

So for each WriteTitleEvent you’ll want an EventName and a Body. As the name would suggest, EventName will be the name of the event you want to write to the dashboard. Body will be all the custom data that you’ll want that said event to have. In this case, we want the event to know what state the player left in and we also want to know the PlayerId of said player.

You might be wondering how you see this data.

For people who are testing out this data to make sure it works, you want to go to the Playstream Monitor

This can be found in the Title Overview

This is a monitor that will allow you to monitor all incoming events to the title in real time. You’ll want to use this because the data that contains all these events have a 5-10 minute delay before updating so the monitor makes it a lot easier and faster to track incoming data traffic.

In order to see all of your written title events you’ll want to go to the Data tab under analytics. Again, 5-10 minute delay between events.

In there you’ll be able to see all of the events you’ve written to PlayFab and be able to query them as well, if you wish to go in depth on certain events which can help with deciding what to change with your game, if something doesn’t add up or is doing better than expected.

That is the bare bone basics of using the Server Api

The client api, is a bit different.

You see, in PlayFab and any traditional engine, you’ll be able to communicate in ways to PlayFab on a client connected to the server. However since roblox handles it’s requests a tad bit differently, the setup for the client api is going to be different than the server api.

For starters, the reason you’ll want to use the client api is because it has engagement and player-based features that the server api does not currently have. For this reason, I would recommend knowing if you want to use the client api at all before integrating PlayFab into your game.

So for the basics, if you want to use the PlayFab api you’ll need to use a different login method. This is essential

Instead of using PlayFabServerApi.LoginWithCustomServerId

You’ll want to use PlayFabClientApi.LoginWithCustomId

Now, remember how I said there’ll be times in which you’ll want to use the success and error callbacks for getting whatever Playfab API may have returned? This is one of those times where it is crucial to use it.

In order to get the Client API to work with roblox. I needed to make it so you could only use the client api with a PlayFab ID this is not to be confused with a roblox userid. The client api only works on individual session tickets, that’s how it was setup to begin with. As such, the PlayFab module needs to contain a list of all of the session tickets.

As such with every client api call, aside from a select few(LoginWithCustomId being one of the select few) you’ll need to add a PlayFabId into the first table parameter

If you want to simply do this, here’s an example code of how.


local playFabIds = {}

local function loginPlayer(userId)
	PlayFabClientApi.LoginWithCustomID({
		CreateAccount = true;
		CustomId = userId;
	},
	
	function(result)
		playFabIds[tostring(userId)] = result.PlayFabId
	end,
	
	function(error)
		print("Failed to login player: ", error.errorMessage)
	end
	)
end

The result in LoginWithCustomID will always return a PlayFabId. You’ll want to cache that somewhere in your code if you plan to use the Client Api. Otherwise the Server Api can handle everything for title data.

You will need to use the client api if you plan to have some stuff for engagement. PlayFab has a lot of engagement features such as a catalog that you can add items to and the Player! And that’s just the start of what it can do.

That’s it for now! If you all think this helped and want me to go in more depth on how all the engagement features and handling player data on playfab. I’ll be more than happy to so please let me know :slight_smile:

24 Likes

Thanks this can come in handy when degerming a popular language for your game diffidently going to use this :+1:

1 Like

How would I add items to the playfab leaderboard area.

Hey! Great question, so the very basics of it is Leaderboards use Player Statistics. You yourself actually won’t need to do much on the leaderboard itself. When you go to the Leaderboard menu in your dashboard and create a new leaderboard, you’ll notice there’s a “StatisitcName” field you need to enter. That is more than just a name, it aligns with the stat, as it implies that the leaderboard will be based off of.

Say you want a “Top Score” leaderboard for example, what you’ll want to do is use the UpdatePlayerStatistics method in the PlayFabServerApi and update the statistic to the exact name of the statistic the leaderboard expects. Here’s an example code:

        PlayFabServerApi.UpdatePlayerStatistics({PlayFabId = playerPlayFabId; Statistics = {{StatisticName = "Score", Value = 50}}}, 
        function(result)
            print("Successfully updated player stats!")
        end,
    
        function(result)
            print("Failed to update player stats", result)
        end)

Here I am adding a statistic called “Score” to the player’s statistic and setting the value to 50. The leaderboard should have the StatisticName field set as “Score” and the dashboard will handle the rest for you :slight_smile:

It’s important to note that you will need the player’s playfab Id which I would recommend either handling it through state management or caching it in a table somewhere. As you’ll recall in the tutorial, the PlayFab Id is returned through the LoginWithServerCustomId method, or LoginWithCustomId if you plan on using the client api.

But you bring up a good topic to cover in a future tutorial where I’ll go more in depth which I’ll get started on later this weekend :slight_smile: so stay tuned for that.

Is there a way I could get a users PlayFab ID with just their UserID?

You’ll need to pass in the PlayFabId to the PlayFab API. The UserId won’t work but you want to pass in the UserId when logging the player in, which in turn will return the PlayFabId on the success callback.

No, like could I get a player’s PlayFabID with their UserID. Like a function where I input a player’s UserID and get their PlayFabID as output.

There is GetPlayFabIDsFromGenericIDs however I’d recommend against it. For the purpose of limiting PlayFab API calls since they do hold weight in the long run. I would suggest caching the PlayFab Id that’s returned from the login method somewhere where it’s easily accessible through all the programs that need the PlayFab API.

1 Like

This is nice, as currently if your game is not popular, you cannot join Roblox’s official PlayFab program here.

Correct and for me, PlayFab is a must for not only analytics but live operations between Engagement and content as well. Analytics is just a bonus for me. I plan to create more tutorials to allow further use of the PlayFab dashboard. :slight_smile:

How do you track a purchase with this module?

Sorry, but bumping this message I am also curious :sweat_smile:

Another question I have is what is the difference between Server and Client Apis? They seem to have the exact same methods, is there any reason we should use one over the other? (Eg. Both PlayFabServerApi and PlayFabClientApi can invoke the WritePlayerEvent method)