ProfileService/Datastore Help

I’ve spent the vast majority of the last few days going through tutorials, making my own script drafts and trying to figure out how I can use the ProfileService module to save the data in my game and I just cannot understand it for the life of me.
I’m pretty desperate as this is the one and only thing that has caused me to write on the forums, I am in need of help to show me how I can save my data (What halos a player has unlocked) and how I can call it to check if I need to make the item visible in game (Screen Ui) and possibly give comments as well.
None of the tutorials I have followed or found have helped me to understand what I need to do.
The Halos all have different unlock methods so I would like to know how I can make the player unlock them and tell the datastore that, right now it currently runs off of the roblox BadgeService (Yeah not good or worth it I know).

1 Like

I’m sure you’ve seen this before after watching those tutorials. What I’ve done is that I’ve added a table into the ProfileTemplate, this table will include all unlocked halos.

-- Updating the template will not include missing template values
--   in existing player profiles!
local ProfileTemplate = {
  UnlockedHalos = {}
}

----- Loaded Modules -----

local ProfileService = require(game.ServerScriptService.ProfileService)

----- Private Variables -----

local Players = game:GetService("Players")

local ProfileStore = ProfileService.GetProfileStore(
    "PlayerData",
    ProfileTemplate
)

local Profiles = {} -- [player] = profile



local function PlayerAdded(player)
    local profile = ProfileStore:LoadProfileAsync("Player_" .. player.UserId)
    if profile ~= nil then
        profile:AddUserId(player.UserId) -- GDPR compliance
        profile:Reconcile() -- Fill in missing variables from ProfileTemplate (optional)
        profile:ListenToRelease(function()
            Profiles[player] = nil
            -- The profile could've been loaded on another Roblox server:
            player:Kick()
        end)
        if player:IsDescendantOf(Players) == true then
            Profiles[player] = profile
            -- A profile has been successfully loaded:
            DoSomethingWithALoadedProfile(player, profile)
        else
            -- Player left before the profile loaded:
            profile:Release()
        end
    else
        -- The profile couldn't be loaded possibly due to other
        --   Roblox servers trying to load this profile at the same time:
        player:Kick() 
    end
end

----- Initialize -----

-- In case Players have joined the server earlier than this script ran:
for _, player in ipairs(Players:GetPlayers()) do
    task.spawn(PlayerAdded, player)
end

----- Connections -----

Players.PlayerAdded:Connect(PlayerAdded)

Players.PlayerRemoving:Connect(function(player)
    local profile = Profiles[player]
    if profile ~= nil then
        profile:Release()
    end
end)

To add a halo to the player you could use a function like this:

local function UnlockHalo(player, halo)
  local profile = Profiles[player] --get the players dataprofile

     if profile then 
       local HalosTable = profile.Data.UnlockedHalos --get the table with the unlocked halos
       table.insert(HalosTable, halo) --add the halo to this table
     end
end

When you want a player to unlock a halo simply call that function

To get the players unlocked halos you could make a function like this:

local function GetPlayerHalos(player)
   local profile = Profiles[player]
   if profile then
       return(profile.Data.UnlockedHalos) --return the table of unlocked halos
   end
end  

You can call these 2 functions for example after triggering a remote event, simply add them in your datahandler script and connect them to a remote or bindable event

Thank you, this definitely helps me to understand the basics a lot more and I feel confident I’ll be able to get what I need done from here.
I appreciate the guidance definitely the best I’ve received!

1 Like

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.