Advertisement
HowToRoblox

ArmorServer

Feb 25th, 2023 (edited)
1,144
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 3.37 KB | None | 0 0
  1. --Types of armor
  2. local armorTypes = {
  3.     "Head";
  4.     "Torso";
  5.     "Hands";
  6.     "Legs";
  7.     "Feet";
  8. }
  9.  
  10. --Modules
  11. local equipFunc = require(script:WaitForChild("EquipArmor"))
  12. local removeFunc = require(script:WaitForChild("RemoveArmor"))
  13. local deleteFunc = require(script:WaitForChild("DeleteArmor"))
  14.  
  15. --RemoteEvents
  16. local remotes = game:GetService("ReplicatedStorage"):WaitForChild("RemoteEvents")
  17. local equipRE = remotes:WaitForChild("EquipArmor")
  18. local removeRE = remotes:WaitForChild("RemoveArmor")
  19. local deleteRE = remotes:WaitForChild("DeleteArmor")
  20.  
  21. --Other variables
  22. local armorpieces = game:GetService("ReplicatedStorage"):WaitForChild("ArmorPieces")
  23.  
  24.  
  25. --Data handling
  26. local dss = game:GetService("DataStoreService")
  27. local ds = dss:GetDataStore("PLAYER ARMOR DATA")
  28.  
  29.  
  30. function saveData(plr:Player)
  31.  
  32.     if not plr:FindFirstChild("DATA FAILED TO LOAD") then
  33.  
  34.         local inventory = {}
  35.         for _, armor in pairs(plr.ArmorInventory:GetChildren()) do
  36.             table.insert(inventory, armor.Name)
  37.         end
  38.         local equipped = {}
  39.         for _, armor in pairs(plr.ArmorEquipped:GetChildren()) do
  40.             if armor.Value ~= nil then
  41.                 equipped[armor.Name] = armor.Value.Name
  42.             else
  43.                 equipped[armor.Name] = nil
  44.             end
  45.         end
  46.         local compiledData = {
  47.             Inventory = inventory;
  48.             Equipped = equipped;
  49.         }
  50.  
  51.         local success, err = nil, nil
  52.         while not success do
  53.             success, err = pcall(function()
  54.                 ds:SetAsync(plr.UserId, compiledData)
  55.             end)
  56.             if err then
  57.                 warn(err)
  58.             end
  59.             task.wait(0.02)
  60.         end
  61.     end
  62. end
  63.  
  64.  
  65. game.Players.PlayerRemoving:Connect(saveData)
  66. game:BindToClose(function()
  67.     for _, plr in pairs(game.Players:GetPlayers()) do
  68.         saveData(plr)
  69.     end
  70. end)
  71.  
  72. game.Players.PlayerAdded:Connect(function(plr)
  73.    
  74.     local dataFailedWarning = Instance.new("StringValue")
  75.     dataFailedWarning.Name = "DATA FAILED TO LOAD"
  76.     dataFailedWarning.Parent = plr
  77.  
  78.     local success, armorData = nil, nil
  79.     while not success do
  80.         success, armorData = pcall(function()
  81.             return ds:GetAsync(plr.UserId)
  82.         end)
  83.         task.wait(0.02)
  84.     end
  85.     dataFailedWarning:Destroy()
  86.    
  87.     if not armorData then
  88.         armorData = {
  89.             Inventory = {"Basic Shoes", "Basic Pants", "Basic Top", "Basic Gloves", "Basic Hat"}; --Armor you will start with
  90.             Equipped = {};
  91.         }
  92.     end
  93.  
  94.     local inv = Instance.new("Folder")
  95.     inv.Name = "ArmorInventory"
  96.     inv.Parent = plr
  97.    
  98.     for _, armorName in pairs(armorData.Inventory) do
  99.        
  100.         for __, armorpiece in pairs(armorpieces:GetDescendants()) do
  101.             if armorpiece.Name == armorName and armorpiece.Parent.Parent == armorpieces then
  102.                 armorpiece:Clone().Parent = inv
  103.                 break
  104.             end
  105.         end
  106.     end
  107.    
  108.     local equipped = Instance.new("Folder")
  109.     equipped.Name = "ArmorEquipped"
  110.     equipped.Parent = plr
  111.    
  112.     for _, armorType  in pairs(armorTypes) do
  113.         local armorValue = Instance.new("ObjectValue")
  114.         armorValue.Name = armorType
  115.         armorValue.Parent = equipped
  116.     end
  117.    
  118.     for armortype, armorname in pairs(armorData.Equipped) do
  119.         if armorname then
  120.             equipFunc(plr, armortype, armorname)
  121.         end
  122.     end
  123.    
  124.     plr.CharacterAdded:Connect(function()
  125.         for _, armorValue in pairs(equipped:GetChildren()) do
  126.             if armorValue.Value ~= nil then
  127.                 equipFunc(plr, armorValue.Name, armorValue.Value.Name, true)
  128.             end
  129.         end
  130.     end)
  131. end)
  132.  
  133.  
  134. --Listening to RemoteEvents
  135. equipRE.OnServerEvent:Connect(equipFunc)
  136. removeRE.OnServerEvent:Connect(removeFunc)
  137. deleteRE.OnServerEvent:Connect(deleteFunc)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement