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
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