I’m currently working on a project using Profile Service to save due to it easiness to save stats and inventorys (skins)
I made this script to use profile service after watching some guides!
local PlayerDataHandler = {}
local datatemplate = {
XP = 0,
Level = 0
}
local profileservice = require(game:GetService("ServerScriptService").ProfileService)
local Players = game:GetService("Players")
local profilestore = profileservice.GetProfileStore(
"PlayerProfile",
datatemplate
)
local profiles = {}
local function playerAdded(player)
local profile = profilestore:LoadProfileAsync("Player_"..player.UserId)
if profile then
profile:AddUserId(player.UserId)
profile:Reconcile()
profile:ListenToRelease(function()
profiles[player] = nil
player:Kick()
end)
if not player:IsDescendantOf(Players) then
profile:Release()
else
profiles[player] = profile
print(profiles[player].Data)
end
else
player:Kick()
end
end
function PlayerDataHandler:Init()
for _, player in game.Players:GetPlayers() do
task.spawn(playerAdded, player)
end
game.Players.PlayerAdded:Connect(playerAdded)
game.Players.PlayerRemoving:Connect(function(player)
if profiles[player] then
profiles[player]:Release()
end
end)
end
local function getProfile(player)
assert(profiles[player], string.format("Profile does not exist for %s", player.UserId))
return profiles[player]
end
--Get/Set Methods
function PlayerDataHandler:Get(player, key)
local profile = getProfile(player)
assert(profile.Data[key], string.format("Data does not exist for key: %s", key))
return profile.Data[key]
end
function PlayerDataHandler:Set(player, key, value)
local profile = getProfile(player)
assert(profile.Data[key], string.format("Data does not exist for key: %s", key))
assert(type(profile.Data[key]) == type(value))
profile.Data[key] = value
end
function PlayerDataHandler:Update(player, key, callback)
local profile = getProfile(player)
local oldData = self:Get(player, key)
local newData = callback(oldData)
self.Set(player, key, newData)
end
return PlayerDataHandler
For some reason it is not working at lines 57! This is pretty bad if you can’t tell!
I’m 90% sure it’s due to assert and producing an error message! If you have a fix to this issue, please respond!