Advertisement
HowToRoblox

PetServer

Mar 9th, 2023
1,178
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 6.07 KB | None | 0 0
  1. --Variables
  2. local dss = game:GetService("DataStoreService")
  3. local ds = dss:GetDataStore("PETS DATASTORE")
  4.  
  5. local rs = game:GetService("ReplicatedStorage")
  6. local pets = rs:WaitForChild("Pets")
  7. local remotes = rs:WaitForChild("RemoteEvents")
  8.  
  9. local rnd = Random.new()
  10.  
  11. local config = require(rs:WaitForChild("CONFIGURATION"))
  12. local add = require(script:WaitForChild("AddPetToInventory"))
  13. local del = require(script:WaitForChild("DeletePetFromInventory"))
  14. local equip = require(script:WaitForChild("EquipPet"))
  15. local unequip = require(script:WaitForChild("UnequipPet"))
  16. local create = require(script:WaitForChild("CreatePet"))
  17.  
  18.  
  19. --Saving and Loading Data
  20. function saveData(plr:Player)
  21.    
  22.     if not plr:FindFirstChild("DATA FAILED TO LOAD") then
  23.        
  24.         local plrCash = plr.leaderstats.Cash.Value
  25.        
  26.         local plrInventory = {}
  27.         for _, pet in pairs(plr.PetsInventory:GetChildren()) do
  28.             table.insert(plrInventory, pet.Value.Name)
  29.         end
  30.        
  31.         local plrEquipped = {}
  32.         for _, pet in pairs(plr.PetsEquipped:GetChildren()) do
  33.             if pet.Value then
  34.                 table.insert(plrEquipped, pet.Value.Name)
  35.             end
  36.         end
  37.        
  38.         local compiledData = {}
  39.         compiledData.Cash = plrCash
  40.         compiledData.Inventory = plrInventory
  41.         compiledData.Equipped = plrEquipped
  42.        
  43.         local success, err = nil, nil
  44.         while not success do
  45.             success, err = pcall(function()
  46.                 ds:SetAsync(plr.UserId, compiledData)
  47.             end)
  48.             if err then
  49.                 warn(err)
  50.             end
  51.             task.wait(0.02)
  52.         end
  53.     end
  54. end
  55.  
  56. game.Players.PlayerRemoving:Connect(saveData)
  57. game:BindToClose(function()
  58.     for _, plr in pairs(game.Players:GetPlayers()) do
  59.         saveData(plr)
  60.     end
  61. end)
  62.  
  63. game.Players.PlayerAdded:Connect(function(plr)
  64.    
  65.     plr.CharacterAdded:Connect(function(char)
  66.        
  67.         if not plr:FindFirstChild("DATA FAILED TO LOAD") then
  68.            
  69.             local equippedPetsContainer = Instance.new("Folder")
  70.             equippedPetsContainer.Name = "EQUIPPED PETS"
  71.             equippedPetsContainer.Parent = char
  72.            
  73.             for _, equippedValue in pairs(plr:WaitForChild("PetsEquipped"):GetChildren()) do
  74.                 if equippedValue.Value then
  75.                    
  76.                     create(plr, equippedValue)
  77.                 end
  78.             end
  79.         end
  80.     end)
  81.  
  82.     local dataFailedWarning = Instance.new("StringValue")
  83.     dataFailedWarning.Name = "DATA FAILED TO LOAD"
  84.  
  85.     local success, plrData = nil, nil
  86.     while true do
  87.         success, plrData = pcall(function()
  88.             return ds:GetAsync(plr.UserId)
  89.         end)
  90.         task.wait(0.02)
  91.         if not success then
  92.             dataFailedWarning.Parent = plr
  93.         else
  94.             break
  95.         end
  96.     end
  97.     dataFailedWarning:Destroy()
  98.  
  99.     if not plrData then
  100.         plrData = {
  101.             Cash = 30000;
  102.             Inventory = {"Dog"};
  103.             Equipped = {};
  104.         }
  105.     end
  106.  
  107.     local ls = Instance.new("Folder")
  108.     ls.Name = "leaderstats"
  109.     ls.Parent = plr
  110.  
  111.     local cashValue = Instance.new("IntValue")
  112.     cashValue.Name = "Cash"
  113.     cashValue.Value = plrData.Cash
  114.     cashValue.Parent = ls
  115.    
  116.     local inventory = Instance.new("Folder")
  117.     inventory.Name = "PetsInventory"
  118.     inventory.Parent = plr
  119.    
  120.     local equipped = Instance.new("Folder")
  121.     equipped.Name = "PetsEquipped"
  122.     equipped.Parent = plr
  123.  
  124.     for _, pet in pairs(plrData.Inventory) do
  125.         local foundPet = pets:FindFirstChild(pet, true)
  126.  
  127.         if foundPet and foundPet.Parent.Parent == pets then
  128.             local petValue = Instance.new("ObjectValue")
  129.             petValue.Value = foundPet
  130.             petValue.Parent = inventory
  131.         end
  132.     end
  133.    
  134.     for i = 1, config.MaxPetsEquipped do
  135.         local equippedValue = Instance.new("ObjectValue")
  136.         equippedValue.Name = i
  137.        
  138.         local linkedPet = Instance.new("ObjectValue")
  139.         linkedPet.Name = "LINKED PET"
  140.         linkedPet.Parent = equippedValue
  141.  
  142.         equippedValue:GetPropertyChangedSignal("Value"):Connect(function()
  143.             local newValue = equippedValue.Value
  144.            
  145.             if linkedPet.Value then
  146.                 linkedPet.Value:Destroy()
  147.             end
  148.  
  149.             if newValue then
  150.                 create(plr, equippedValue)
  151.             end
  152.         end)
  153.        
  154.         if plrData.Equipped[i] then
  155.             local foundPet = pets:FindFirstChild(plrData.Equipped[i], true)
  156.            
  157.             if foundPet and foundPet.Parent.Parent == pets then
  158.                 equippedValue.Value = foundPet
  159.             end
  160.         end
  161.         equippedValue.Parent = equipped
  162.     end
  163. end)
  164.  
  165.  
  166. --Deleting Pets
  167. remotes:WaitForChild("DeletePet").OnServerEvent:Connect(del)
  168.  
  169. --Equipping Pets
  170. remotes:WaitForChild("EquipPet").OnServerEvent:Connect(equip)
  171.  
  172. --Unequipping Pets
  173. remotes:WaitForChild("UnequipPet").OnServerEvent:Connect(unequip)
  174.  
  175.  
  176. --Setting up Incubators
  177. for _, incubator in pairs(workspace:WaitForChild("Incubators"):GetChildren()) do
  178.    
  179.     local prompt = Instance.new("ProximityPrompt")
  180.     prompt.ObjectText = incubator.Name
  181.     prompt.ActionText = "View pets"
  182.     prompt.HoldDuration = 0
  183.     prompt.KeyboardKeyCode = Enum.KeyCode.F
  184.     prompt.RequiresLineOfSight = false
  185.     prompt.MaxActivationDistance = 7
  186.     prompt.Parent = incubator.PrimaryPart or incubator
  187.    
  188.     prompt.Triggered:Connect(function(plr)
  189.         remotes:WaitForChild("ViewIncubator"):FireClient(plr, incubator)
  190.     end)
  191. end
  192.  
  193. --Hatching Pets
  194. remotes:WaitForChild("HatchPet").OnServerEvent:Connect(function(plr, incubator)
  195.     if plr and incubator and incubator.Parent == workspace.Incubators then
  196.        
  197.         local incubatorConfig = require(incubator.Configuration)
  198.        
  199.         if plr.leaderstats.Cash.Value >= incubatorConfig.Price and #plr.PetsInventory:GetChildren() < config.MaxPetsInventory then
  200.             plr.leaderstats.Cash.Value -= incubatorConfig.Price
  201.            
  202.             local chances = incubatorConfig.Chances
  203.             local plrChance = rnd:NextNumber() * 100
  204.  
  205.             local n = 0
  206.             local rarityChosen = nil
  207.  
  208.             for rarity, chance in pairs(chances) do
  209.                 n += chance
  210.                 if plrChance <= n then
  211.                     rarityChosen = rarity
  212.                     break
  213.                 end
  214.             end
  215.  
  216.             local hatchablePets = incubatorConfig.HatchablePets
  217.  
  218.             for i = #hatchablePets, 2, -1 do
  219.                 local j = rnd:NextInteger(1, i)
  220.                 hatchablePets[i], hatchablePets[j] = hatchablePets[j], hatchablePets[i]
  221.             end
  222.            
  223.             local petChosen = nil
  224.  
  225.             for _, petName in pairs(hatchablePets) do
  226.                 if pets:FindFirstChild(petName, true) and pets:FindFirstChild(petName, true).Parent.Name == rarityChosen then
  227.                     petChosen = petName
  228.                     break
  229.                 end
  230.             end
  231.            
  232.             add(plr, petChosen)
  233.            
  234.             remotes:WaitForChild("HatchPet"):FireClient(plr, petChosen)
  235.         end    
  236.     end
  237. end)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement