Hello developers,
I’ve been trying to use ProfileService to save players’ items when they leave, and then give them back when the player rejoins the game.
I first placed all the items in the player’s inventory into a table, then when the player rejoins, I clone the items from server storage into the player’s inv.
The problem is sometimes the server doesn’t clone the weapons into the player inventory, even though the server identifies the items inside the table. (printed table successfully)
local Players = game:GetService("Players")
local ServerScriptService = game:GetService("ServerScriptService")
local ServerStorage = game:GetService("ServerStorage")
local Template = require(ServerScriptService.PlayerData.Template)
local Manager = require(ServerScriptService.PlayerData.Manager)
local ProfileService = require(ServerScriptService.Libs.ProfileService)
local ProfileStore = ProfileService.GetProfileStore("Test4", Template)
local WeaponsFolder = ServerStorage.Weapons
local function LoadWeapons(player: Player)
local profile = Manager.Profiles[player]
if not profile then return end
for _, Tools in pairs(profile.Data.Weapons) do
if not WeaponsFolder:FindFirstChild(Tools.ToolName) then continue end
local Tool = WeaponsFolder[Tools.ToolName]:Clone()
Tool.Parent = player:WaitForChild("Backpack")
end
print(profile.Data.Weapons)
table.clear(profile.Data.Weapons)
end
local function LoadProfile(player: Player)
local profile = ProfileStore:LoadProfileAsync("Player_"..player.UserId)
if not profile then
player:Kick()
return
end
profile:AddUserId(player.UserId)
profile:Reconcile()
profile:ListenToRelease(function()
Manager.Profiles[player] = nil
player:Kick()
end)
if player:IsDescendantOf(Players) == true then
Manager.Profiles[player] = profile
LoadWeapons(player)
else
profile:Release()
end
end
for _, player in Players:GetPlayers() do
task.spawn(LoadProfile, player)
end
Players.PlayerAdded:Connect(LoadProfile)
Players.PlayerRemoving:Connect(function(player)
local profile = Manager.Profiles[player]
for _,tool in pairs (player.Backpack:GetChildren()) do
if not tool:IsA("Tool") then continue end
if WeaponsFolder:FindFirstChild(tool.Name) then
table.insert(profile.Data.Weapons, {ToolName = tool.Name, Ammo = 44})
end
end
if profile then
profile:Release()
end
end)
Can you try print its FullName to see where it went?
for _, Tools in pairs(profile.Data.Weapons) do
if not WeaponsFolder:FindFirstChild(Tools.ToolName) then continue end
local Tool = WeaponsFolder[Tools.ToolName]:Clone()
Tool.Parent = player:WaitForChild("Backpack")
print(Tool:GetFullName())
end
This seems unlikely but does the weapon has Archivable on? (If it didn’t it’d be weird how it cloned successfully in the first place, but it’s good to check anyways)
Yup, Archivable is set to true. I’ve also tested a few times, and it seems like the chance of :Clone() failing is very random, sometimes it usually fails when i have 5 items, but other times it takes a lot of tries when I have 7 items.
This is a roblox issue, I created a similar script simulating yours and it didn’t work because Backpack probably couldn’t load right away. You have to add some yield to it.
local Players = game:GetService("Players")
local ReplicatedStorage = game:GetService("ReplicatedStorage")
local Profiles = {}
local WeaponsFolder = ReplicatedStorage:WaitForChild("WeaponsFolder")
local function LoadWeapons(player: Player)
local profile = Profiles[player]
if not profile then return end
for _, Tools in pairs(profile.Data.Weapons) do
if not WeaponsFolder:FindFirstChild(Tools.ToolName) then continue end
local Tool = WeaponsFolder[Tools.ToolName]:Clone()
Tool.Parent = player:WaitForChild("Backpack")
end
print(profile.Data.Weapons)
table.clear(profile.Data.Weapons)
end
local function GenerateProfile()
local tab = {Data = {Weapons = {}}}
for i = 1, math.random(2, 10) do
table.insert(tab.Data.Weapons, {ToolName = "ClassicSword", Ammo = 44})
end
return tab
end
Players.PlayerAdded:Connect(function(player)
Profiles[player] = GenerateProfile()
-- If you do it instantly it doesn't work, try commenting the wait
task.wait(5)
LoadWeapons(player)
end)
repro.rbxl (53.5 KB)
If it still doesn’t work you can check my file, but it should work if you add some kind of yielding (task.wait(3)) right before the LoadWeapons function
There’s also another issue of this code, if the player equip a sword, it is no longer inside the Backpack and thus, won’t be saved. You need to loop through the player as well (or :FindFirstChildWhichIsA("Tool").