I making RPG but item saving system doesnt works sometimes
But Inventory saving system works (inventory gui) only backpack Saving not works (and Holding Tool)
Heres code:
local DSS = game:GetService("DataStoreService")
local InvDS = DSS:GetDataStore("Inventory")
local EqDS = DSS:GetDataStore("Equipped")
local CashDS = DSS:GetDataStore("Cash")
local function SetPack(player)
for _,v in ipairs(player.StarterGear:GetChildren()) do
v:Destroy()
end
local Tools = {}
for _,v in ipairs(player.Character:GetChildren()) do
if v:IsA("Tool") then
local VC = game.ReplicatedStorage.ToolLibrary:FindFirstChild(v.Name):Clone()
table.insert(Tools,VC)
end
end
for _,v in ipairs(player.Backpack:GetChildren()) do
if v:IsA("Tool") then
local VC = game.ReplicatedStorage.ToolLibrary:FindFirstChild(v.Name):Clone()
table.insert(Tools,VC)
end
end
for _,v in ipairs(Tools) do
v.Parent = player.StarterGear
end
pcall(function()
for _,v in ipairs(game.ReplicatedStorage.PlayerValues:FindFirstChild(player.Name.."|Values").Equipping:GetChildren()) do
v:Destroy()
end
for _,v in ipairs(player.StarterGear:GetChildren()) do
local Int = Instance.new("IntValue",game.ReplicatedStorage.PlayerValues:FindFirstChild(player.Name.."|Values").Equipping)
Int.Name = v.Name
end
end)
end
game.Players.PlayerAdded:Connect(function(plr)
local Values = script.Values:Clone()
Values.Name = plr.Name.."|Values"
Values.Parent = game.ReplicatedStorage.PlayerValues
local SavedInventory = InvDS:GetAsync(plr.UserId) or {"Basic Sword","Basic Pickaxe"}
for _,v in ipairs(SavedInventory) do
local Int = Instance.new("IntValue",Values.Inventory)
Int.Name = v
end
local SavedEquipped = EqDS:GetAsync(plr.UserId) or {}
for _,v in ipairs(SavedEquipped) do
local T = game.ReplicatedStorage:WaitForChild("ToolLibrary"):FindFirstChild(v):Clone()
T.Parent = plr.Backpack
print("Joining: "..v)
end
print(SavedEquipped)
local ls = Instance.new("Folder",plr)
ls.Name = "leaderstats"
local Cash = Instance.new("IntValue",ls)
Cash.Name = "Cash"
Cash.Value = CashDS:GetAsync(plr.UserId) or 100
local c = coroutine.wrap(function()
local Success
repeat task.wait(.5)
Success = pcall(function()
SetPack(plr)
end)
until not Success
end)
c()
end)
game.Players.PlayerRemoving:Connect(function(plr)
local FR = game.ReplicatedStorage:FindFirstChild("PlayerValues"):FindFirstChild(plr.Name.."|Values")
if #game.Players:GetPlayers() <= 0 then return end
local s,e = pcall(function()
local SavedInv = {}
for _,v in ipairs(FR:FindFirstChild("Inventory"):GetChildren()) do
table.insert(SavedInv,v.Name)
end
local SavedEquip = {}
for _,v in ipairs(FR:FindFirstChild("Equipping"):GetChildren()) do
table.insert(SavedEquip,v.Name)
print("Leaving: "..v.Name)
end
if FR then
FR:Destroy()
print("Folder Destroyed")
else
warn("Player folder not found")
end
EqDS:SetAsync(plr.UserId,SavedEquip)
CashDS:SetAsync(plr.UserId,plr:FindFirstChild("leaderstats").Cash.Value)
InvDS:SetAsync(plr.UserId,SavedInv)
end)
if not s then
print("We got error while saving:")
warn(e)
end
end)
game:BindToClose(function()
local PlrV = game.ReplicatedStorage.PlayerValues:Clone()
PlrV.Parent = script
for _,v in ipairs(PlrV:GetChildren()) do
local PlrName = string.split(v.Name,"|")[1]
local SavedInv = {}
for _,v in ipairs(v.Inventory:GetChildren()) do
table.insert(SavedInv,v.Name)
end
local SavedEquip = {}
for _,v in ipairs(v.Equipping:GetChildren()) do
table.insert(SavedEquip,v.Name)
end
local UserId = game.Players:GetUserIdFromNameAsync(PlrName)
InvDS:SetAsync(UserId,SavedInv)
EqDS:SetAsync(UserId,SavedEquip)
end
end)
Saving system not working when leave, game shutdown (BindToClose)
We tried this problem like 2 weeks but still CANT fix it
Its kinda weird its sometimes doesnt saves
Please help!