So I am making an Obby in which players can buy items from the shop. Once they buy the item, it saves in their backpack and if they leave the game, the item/items which they have also get saved and load into their backpack once they rejoin. But the problem is that if a player leaves the game while equipping an item, the next time they join the game, their item which was equipped is lost.
How to fix this?
I have a toolSave script inside ServerScriptService which is as follows:
local ds = game:GetService("DataStoreService"):GetDataStore("ToolSave")
game.Players.PlayerAdded:connect(function(plr)
local key = "id-"..plr.userId
pcall(function()
local tools = ds:GetAsync(key)
if tools then
for i,v in pairs(tools) do
local tool = game.ServerStorage.Tools:FindFirstChild(v)
if tool then
tool:Clone().Parent = plr:FindFirstChild("Backpack")
tool:Clone().Parent = plr:FindFirstChild("StarterGear")
end
end
end
end)
end)
game.Players.PlayerRemoving:connect(function(plr)
local key = "id-"..plr.userId
local toolsToSave = {}
for i,v in pairs(plr.Backpack:GetChildren()) do
if v then
table.insert(toolsToSave,i,v.Name)
end
end
ds:SetAsync(key,toolsToSave)
local char = plr.Character
if char then
local charParts = char:GetChildren()
for key,part in (charParts) do
if part.IsA("Tool") then
table.insert(toolsToSave,part)
end
end
end
end)
Please tell me what I am doing wrong.
Thanks

4 comments
I agree!
So should I do on line 20 like this inside the loop "plr.Backpack, plr.StarterGear:GetChildren()"?
just get the children of the StarterGear instead of Backpack
Thanks. That worked.