Scripting Helpers is winding down operations and is now read-only. More info→
Ad
Log in to vote
0

How can I create an inventory save system for a tool with different values?

Asked by 5 years ago
Edited by DanzLua 5 years ago

I'd just like to say that I'm a beginner and so I rely on tutorials and such, and don't have a great understanding of datastore yet. But the tools I want to save have defined number values, they are the same tool, but for instance, one would deal more damage than the other. The values are randomly generated and so I don't think it'd be worth the effort to create many of the same tools and add them in one folder in Server Storage.

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:WaitForChild("Backpack")
     tool:Clone().Parent = plr:WaitForChild("StarterGear")
    end
   end
  end
 end)
end)
game.Players.PlayerRemoving:connect(function(plr)
 local key = "id-"..plr.userId
 pcall(function()
  local toolsToSave = {}
  for i,v in pairs(plr.Backpack:GetChildren()) do
   if v then
    table.insert(toolsToSave,v.Name)
   end
  end
  ds:SetAsync(key,toolsToSave)
 end)
end)
0
please make this easier to read ReallyUnikatni 68 — 5y

Answer this question