hi! so i heard using profileservice is better then using datastoreservice so i wanted to try
i followed a tutorial and it ended up not working for me, i double checked everything but it just wouldnt work for me
the error is “profile not found for player …”
(this is another module script, not profile service btw)
local PlayerDataHandler = {}
local dataTemplate = {
Inventory = {}
}
local ProfileService = require(game.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
end
if player.Character then
loadBackpack(player)
end
player.CharacterAdded:Connect(function()
loadBackpack(player)
end)
else
player:Kick()
end
end
function loadBackpack(player)
local inventory = PlayerDataHandler:Get(player, "Inventory")
for _, item in inventory do
local asset = game.ServerStorage.Items:FindFirstChild(item)
if asset then
asset:Clone().Parent = player.Backpack
end
end
end
function PlayerDataHandler:Init()
for _, player in 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], "Profile not found for player "..player.Name)
return Profiles[player]
end
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:AddToInventory(player, itemName)
self:Update(player, "Inventory", function(current)
table.insert(current, itemName)
return current
end)
local item = game.ServerStorage.Items:FindFirstChild(itemName)
if not item then return end
item:Clone().Parent = player.Backpack
end
function PlayerDataHandler:RemoveFromInventory(player, itemName)
self:Update(player, "Inventory", function(current)
if table.find(current, itemName) then
table.remove(current, table.find(current, itemName))
end
return current
end)
if player.Backpack:FindFirstChild(itemName) then
player.Backpack[itemName]:Destroy()
elseif player.Character:FindFirstChild(itemName) then
player.Character[itemName]:Destroy()
end
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
```
(line 75 is at the getprofile function)