i’m new on Datastore but i was able to make script work its for inventory table:
local InvChecks = require(game:GetService("ReplicatedStorage").LootSys)
local RunService = game:GetService("RunService")
local ToolData = game:GetService("DataStoreService"):GetDataStore("ToolData")
local Repl = game:GetService("ReplicatedStorage")
local ToolLocation = game.ServerStorage:WaitForChild("Items") -- The location where all your tools are saved
function GetToolByName(toolName)
for _, tool in pairs(ToolLocation:GetChildren()) do
if tool.Name == toolName then return tool end
end
end
function IsRegisteredTool(tool)
for _, playerTool in pairs(ToolLocation:GetChildren()) do
if playerTool.Name == tool.Name then return true end
end
end
game.Players.PlayerAdded:Connect(function(player)
repeat wait() until player.Character
loadData(player)
print("Load")
end)
game.Players.PlayerRemoving:Connect(function(player)
-- saveData(player)
-- print("Saveing...")
end)
Repl:WaitForChild("Sav").OnServerEvent:Connect(function(player)
print("Saveing...")
saveData(player)
end)
function saveData(player)
local plrdata = {}
local Ctools = player:WaitForChild("PlayerGui").Inventory:GetDescendants()
local playerinv = {}
-- wait(1)
-- InvChecks.checkInvStarter(player.Character) --//Start Rest The Save so it will not duble
-- ToolData:SetAsync(player.userId, {}) --//End Rest
-- plrdata.Cash = player:WaitForChild("Cash").Value --//Cash Save Here
for i,v in pairs(Ctools) do
if v.ClassName == "Tool" then
if v.Name ~= "" or v.Name ~= nil then
-- table.insert(playerinv,{v.Name,v.Value})
if playerinv[v.Name] then
print("There is Already item with name: ".. v.Name)
local maths = i
playerinv[v.Name..maths] = {}
local lb = playerinv[v.Name..maths]
lb.Na = v.Name
lb.Stat = v.Stat.Value
lb.Qty = v.Qty.Value
else
playerinv[v.Name] = {}
local lb = playerinv[v.Name]
lb.Na = v.Name
lb.Stat = v.Stat.Value
lb.Qty = v.Qty.Value
end
end
end
if v.ClassName == "BoolValue" then
if v.Parent.Name == "PrimWeapn" or v.Parent.Name == "SecWeapn" then
if v.Name == "Act" and v.Value == true then
if playerinv[v.Parent.WName.Value] then
print("There is Already item with name: ".. v.Parent.WName.Value)
local maths = i
playerinv[v.Parent.WName.Value..maths] = {}
local lb = playerinv[v.Parent.WName.Value..maths]
lb.Na = v.Parent.WName.Value
lb.Stat = "Equip"
lb.Qty = 1
else
playerinv[v.Parent.WName.Value] = {}
local lb = playerinv[v.Parent.WName.Value]
lb.Na = v.Parent.WName.Value
lb.Stat = "Equip"
lb.Qty = 1
end
end
end
end
end
-- plrdata = playerinv
for i,v in pairs(playerinv) do
print(v.Na.." - "..v.Stat)
end
local Succeded, wrng = pcall(function()
ToolData:SetAsync(player.userId, playerinv) --// sometime this part will not work and the game will not save???!
end)
if Succeded then print("Saved") else print("Didn't Save") end
end
function loadData(player)
repeat wait() until player.Character
local Ctools = player.PlayerGui:WaitForChild("Inventory").BK.Backpack
if ToolData:GetAsync(player.userId) then
local getAsy = ToolData:GetAsync(player.userId)
wait(getAsy)
for i, toolName in pairs(getAsy) do
print(toolName.Na)
if toolName.Na ~= "" or nil then
local newTempTool = ToolLocation[toolName.Na]:Clone()
newTempTool.Parent = Ctools
newTempTool.Qty.Value = toolName.Qty
newTempTool.Stat.Value = toolName.Stat
if newTempTool.Stat.Value == "Inv" then
Ctools.Parent.Parent.CurWgh.Value = Ctools.Parent.Parent.CurWgh.Value + newTempTool.wght.Value
end
if newTempTool.Stat.Value == "Equip" and newTempTool.Type.Value == "PrimW" then
print(newTempTool.Name.." Equipped")
local startT = ToolLocation[toolName.Na]:Clone()
newTempTool.Parent = player.Backpack
startT.Parent = game.StarterPack
end
if newTempTool.Stat.Value == "Equip" and newTempTool.Type.Value == "SecW" then
print(newTempTool.Name.." Equipped")
local startT = ToolLocation[toolName.Na]:Clone()
newTempTool.Parent = player.Backpack
startT.Parent = game.StarterPack
end
if newTempTool.Stat.Value == "Equip" and newTempTool.Type.Value == "Helm" then
print(newTempTool.Name.." Equipped")
newTempTool.Parent = Ctools.Parent.EqupFrame.Head.HBack
end
if newTempTool.Stat.Value == "Equip" and newTempTool.Type.Value == "Chest" then
print(newTempTool.Name.." Equipped")
newTempTool.Parent = Ctools.Parent.EqupFrame.Chest.CBack
end
if newTempTool.Stat.Value == "Equip" and newTempTool.Type.Value == "Back" then
print(newTempTool.Name.." Equipped")
Ctools.Parent.Parent.MaxWgh.Value = Ctools.Parent.Parent.MaxWgh.Value + newTempTool.Use.Value
newTempTool.Parent = Ctools.Parent.EqupFrame.Back.BBack
end
end
end
wait(1)
InvChecks.checkInvStarter(player.Character)
-- ToolData:SetAsync(player.userId, {})
end
end
but sometime it save and sometime it don’t any idea
any advice will be great?
thank you