Advertisement
95Roadkill

Roblox, Saving Inventory.

Dec 12th, 2015
7,874
-1
Never
1
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 6.28 KB | None | 0 1
  1. local ItemInfo = require(script.Parent.Items)
  2. local DataStore = game:GetService("DataStoreService"):GetDataStore("ItemsAndStats")
  3.  
  4. game.Players.PlayerAdded:connect(function(player)
  5.     if player:WaitForDataReady() then
  6.         game.StarterGui.ResetPlayerGuiOnSpawn = false
  7.         print("Creating " .. player.Name .. "'s inventory.")
  8.         local PlayerDir = game.Players:FindFirstChild(player.Name)
  9.         local Gui = PlayerDir.PlayerGui.ScreenGui
  10.         local MaxSlots = 28
  11.         local CurrentSlot = 0
  12.         local Row = 0
  13.         local Line = 0
  14.         local Increment = 50
  15.         Gui.LoadingScreen.Visible = true
  16.         local Inventory = Instance.new("IntValue",player)
  17.         Inventory.Name = "Inventory"
  18.         while MaxSlots > CurrentSlot do
  19.             local GuiIncrement = UDim2.new(0,50*Row,0,50*Line)
  20.             local BarIncrement = 14 * CurrentSlot
  21.             if CurrentSlot < MaxSlots then
  22.                 CurrentSlot = CurrentSlot + 1
  23.                 Row = Row + 1
  24.                 local Slot = Instance.new("Frame",Gui.Inventory)
  25.                     Slot.Name = "Slot" .. CurrentSlot
  26.                     Slot.Size = UDim2.new(0,50,0,50)
  27.                     Slot.Position = GuiIncrement
  28.                     Slot.BorderSizePixel = 0
  29.                 local Item = Instance.new("IntValue",Inventory)
  30.                     Item.Name = "Item" .. CurrentSlot
  31.                 local ItemSlot = Instance.new("IntValue",Gui.Inventory["Slot"..CurrentSlot])
  32.                     ItemSlot.Name = "Slot"
  33.                     ItemSlot.Value = CurrentSlot
  34.                 local ItemIdent = Instance.new("IntValue",player.Inventory["Item" .. CurrentSlot])
  35.                     ItemIdent.Name = "Id"
  36.                 Gui.LoadingScreen.LoadingBarInner.Size = UDim2.new (0,10+BarIncrement,0,20)
  37.                 Gui.LoadingScreen.LoadingLabel.Text = "Loading: Item_" .. CurrentSlot
  38.                 if DataStore:GetAsync(player.Name .. "Item_"..CurrentSlot) then
  39.                     if DataStore:GetAsync(player.Name .. "Item_"..CurrentSlot) >= 0 then
  40.                                 Item.Value = DataStore:GetAsync(player.Name .. "Item_"..CurrentSlot)
  41.                         local ItemValue = Instance.new("IntValue",Gui.Inventory["Slot"..CurrentSlot])
  42.                         ItemValue.Name = "Item"
  43.                         ItemValue.Value = DataStore:GetAsync(player.Name .. "Item_"..CurrentSlot)
  44.                     end
  45.                 else
  46.                     local ItemValue = Instance.new("IntValue",Gui.Inventory["Slot"..CurrentSlot])
  47.                         ItemValue.Name = "Item"
  48.                         ItemValue.Value = 0
  49.                 end
  50.                 if DataStore:GetAsync(player.Name .. "ItemId_" .. CurrentSlot) then
  51.                     if DataStore:GetAsync(player.Name .. "ItemId_" .. CurrentSlot) > 0 then
  52.                         ItemIdent.Value = DataStore:GetAsync(player.Name .. "ItemId_" .. CurrentSlot)
  53.                         local Ident = Instance.new("IntValue",Gui.Inventory["Slot"..CurrentSlot])
  54.                         Ident.Name = "Id"
  55.                         Ident.Value = DataStore:GetAsync(player.Name .. "ItemId_" .. CurrentSlot)
  56.                         print("Loaded " .. ItemInfo[Ident.Value].Name .. " With a quantity of " .. DataStore:GetAsync(player.Name .. "Item_" .. CurrentSlot) .. " With an Id Value of " ..  DataStore:GetAsync(player.Name .. "ItemId_" .. CurrentSlot) .. "." )
  57.                     else
  58.                         ItemIdent.Value = 0
  59.                         print("Slot " .. CurrentSlot)
  60.                     end
  61.                     if DataStore:GetAsync(player.Name .. "Item_" .. CurrentSlot) > 0 then
  62.                                 if DataStore:GetAsync(player.Name .. "Item_" .. CurrentSlot) > 1 then
  63.                         local StackNumber = Instance.new("TextLabel",Gui.Inventory["Slot" .. CurrentSlot])
  64.                             StackNumber.Name = "StackNumberLabel"
  65.                             StackNumber.Position = UDim2.new(0,13,0,10)
  66.                             StackNumber.FontSize = "Size8"
  67.                             StackNumber.Text =  DataStore:GetAsync(player.Name .. "Item_" .. CurrentSlot)
  68.                             if Inventory["Item"..CurrentSlot].Value >= 1000 and Inventory["Item"..CurrentSlot].Value <= 999999 then
  69.                                 player.PlayerGui.ScreenGui.Inventory["Slot"..CurrentSlot].StackNumberLabel.Text = string.sub(Inventory["Item"..CurrentSlot].Value,1,1) .. "k"
  70.                             if Inventory["Item"..CurrentSlot].Value >= 10000 then
  71.                                 player.PlayerGui.ScreenGui.Inventory["Slot"..CurrentSlot].StackNumberLabel.Text = string.sub(Inventory["Item"..CurrentSlot].Value,1,2) .. "k"
  72.                             end
  73.                             if Inventory["Item"..CurrentSlot].Value >= 100000 then
  74.                                 player.PlayerGui.ScreenGui.Inventory["Slot"..CurrentSlot].StackNumberLabel.Text = string.sub(Inventory["Item"..CurrentSlot].Value,1,3) .. "k"
  75.                             end
  76.                             elseif Inventory["Item"..CurrentSlot].Value >= 1000000 and Inventory["Item"..CurrentSlot].Value < 1000000000 then
  77.                                 player.PlayerGui.ScreenGui.Inventory["Slot"..CurrentSlot].StackNumberLabel.Text = string.sub(Inventory["Item"..CurrentSlot].Value,1,1) .. "m"
  78.                                 player.PlayerGui.ScreenGui.Inventory["Slot"..CurrentSlot].StackNumberLabel.TextColor3 = Color3.new(0, 255, 0)
  79.                             else
  80.                                 player.PlayerGui.ScreenGui.Inventory["Slot"..CurrentSlot].StackNumberLabel.Text = Inventory["Item"..CurrentSlot].Value
  81.                             end
  82.                         end
  83.                         if DataStore:GetAsync(player.Name .. "ItemId_" .. CurrentSlot) > 0 then
  84.                             local Img = Instance.new("ImageButton",Gui.Inventory["Slot"..CurrentSlot])
  85.                             Img.Size = UDim2.new(0,50,0,50)
  86.                             Img.Image = ItemInfo[DataStore:GetAsync(player.Name .. "ItemId_" .. CurrentSlot)].UsedImage
  87.                             Img.Name = "ItemButton"
  88.                             Img.BorderSizePixel = 0
  89.                         end
  90.                     end
  91.                 end
  92.                 if Row == 4 then
  93.                     Row = 0
  94.                     Line = Line + 1
  95.                 end
  96.                 if CurrentSlot == MaxSlots then
  97.                     print(player.Name .. "'s inventory has been created.")
  98.                 end
  99.             end
  100.             wait(0.01)
  101.         end
  102.     Gui.LoadingScreen.Visible = false
  103.     Gui.ResetInventory.Visible = true
  104.     end
  105. end)
  106.  
  107. while true do
  108.     for i,v in pairs (game.Players:GetPlayers()) do
  109.         print("Saving")
  110.         v.PlayerGui.ScreenGui.Saving.Visible = true
  111.         v.PlayerGui.ScreenGui.Saving.Text = "Saving! This may take a while!"
  112.         local MaxItems = 28
  113.         local CurrentItem = 0
  114.         while CurrentItem < MaxItems do
  115.             CurrentItem = CurrentItem + 1
  116.             DataStore:SetAsync(v.Name .. "Item_" .. CurrentItem, v.Inventory["Item"..CurrentItem].Value)
  117.             if v.Inventory["Item"..CurrentItem].Value >= 1 then
  118.                 DataStore:SetAsync(v.Name .. "ItemId_" .. CurrentItem, v.Inventory["Item"..CurrentItem].Id.Value)
  119.                 print("Saved " .. ItemInfo[v.Inventory["Item"..CurrentItem].Id.Value].Name .. " With a quantity of " .. v.Inventory["Item"..CurrentItem].Value .. " and an ID Value of " .. v.Inventory["Item"..CurrentItem].Id.Value .. ".")
  120.             else
  121.                 print("No item in Slot " .. CurrentItem)
  122.             end
  123.             if CurrentItem == MaxItems then
  124.                 v.PlayerGui.ScreenGui.Saving.Text = "Saved"
  125.                 wait(2)
  126.                 v.PlayerGui.ScreenGui.Saving.Visible = false
  127.             end
  128.         end
  129.     end
  130.     wait(120)
  131. end
Advertisement
Comments
  • User was banned
Add Comment
Please, Sign In to add comment
Advertisement