Advertisement
HowToRoblox

PetSystem

Sep 25th, 2021
3,438
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 3.73 KB | None | 0 0
  1. local dss = game:GetService("DataStoreService")
  2. local ds = dss:GetDataStore("datastore")
  3.  
  4.  
  5. function saveData(plr)
  6.    
  7.     local cash = plr.leaderstats.Cash.Value
  8.    
  9.     local ownedPets = {}
  10.    
  11.     for i, pet in pairs(plr.OwnedPets:GetChildren()) do
  12.        
  13.         table.insert(ownedPets, pet.Name)
  14.     end
  15.    
  16.     ds:SetAsync(plr.UserId .. "PETS", ownedPets)
  17.     ds:SetAsync(plr.UserId .. "CASH", cash)
  18. end
  19.  
  20.  
  21.  
  22. game.Players.PlayerAdded:Connect(function(plr)
  23.    
  24.    
  25.     plr.CharacterAdded:Connect(function(char)
  26.  
  27.         char.ChildAdded:Connect(function(child)
  28.            
  29.             if child.Name == "EquippedPet" then
  30.                
  31.                
  32.                 local bp = Instance.new("BodyPosition", child)
  33.                 local bg = Instance.new("BodyGyro", child)
  34.                
  35.                 bp.MaxForce = Vector3.new(math.huge, math.huge, math.huge)
  36.                 bg.MaxTorque = Vector3.new(math.huge, math.huge, math.huge)
  37.                
  38.                 local hrp = char.HumanoidRootPart
  39.                
  40.                
  41.                 while child.Parent == char do
  42.                    
  43.                     wait()
  44.                    
  45.                     bp.Position = hrp.Position - (hrp.CFrame.RightVector * 5)
  46.  
  47.                     bg.CFrame = CFrame.new(hrp.Position, hrp.CFrame.LookVector * 10000)
  48.                 end
  49.             end
  50.         end)
  51.     end)
  52.    
  53.    
  54.     local ls = Instance.new("Folder", plr)
  55.     ls.Name = "leaderstats"
  56.  
  57.     local cash = Instance.new("IntValue", ls)
  58.     cash.Name = "Cash"
  59.    
  60.    
  61.     local ownedPets = Instance.new("Folder", plr)
  62.     ownedPets.Name = "OwnedPets"
  63.    
  64.    
  65.     pcall(function()
  66.        
  67.         local petsData = ds:GetAsync(plr.UserId .. "PETS")
  68.         local cashData = ds:GetAsync(plr.UserId .. "CASH")
  69.        
  70.         cash.Value = cashData or 10000
  71.        
  72.        
  73.         for i, petName in pairs(petsData) do
  74.            
  75.             for x, descendant in pairs(game.ReplicatedStorage:WaitForChild("Pets"):GetDescendants()) do
  76.                
  77.                 if descendant.Name == petName then
  78.                    
  79.                    
  80.                     descendant:Clone().Parent = ownedPets
  81.                 end
  82.             end
  83.         end
  84.     end)
  85. end)
  86.  
  87.  
  88. game.Players.PlayerRemoving:Connect(saveData)
  89.  
  90. game:BindToClose(function()
  91.    
  92.     for i, plr in pairs(game.Players:GetPlayers()) do
  93.        
  94.         saveData(plr)
  95.     end
  96. end)
  97.  
  98.  
  99.  
  100. for i, descendant in pairs(workspace:GetDescendants()) do
  101.    
  102.     if descendant:FindFirstChild("Properties") then
  103.        
  104.        
  105.         local price = descendant.Properties.Price.Value
  106.        
  107.         descendant.BuyButton.BuyGui.BuyTextLabel.Text = "Buy for " .. price
  108.        
  109.        
  110.         local rarities = {}
  111.        
  112.         for x, rarity in pairs(descendant.Properties.Rarities:GetChildren()) do
  113.            
  114.             rarities[rarity.Name] = rarity.Value
  115.         end
  116.        
  117.        
  118.         local pets = {}
  119.        
  120.         for x, rarityFolder in pairs(game.ReplicatedStorage.Pets[descendant.Name]:GetChildren()) do
  121.            
  122.             local percent = rarities[rarityFolder.Name]
  123.            
  124.             for y, pet in pairs(rarityFolder:GetChildren()) do
  125.                
  126.                 for z = 1, percent do
  127.                     table.insert(pets, pet)
  128.                 end
  129.             end
  130.         end
  131.        
  132.        
  133.         descendant.BuyButton.ClickDetector.MouseClick:Connect(function(plr)
  134.            
  135.            
  136.             if plr.leaderstats.Cash.Value >= price then
  137.                
  138.                 plr.leaderstats.Cash.Value -= price
  139.            
  140.                 local randomPet = pets[math.random(1, #pets)]
  141.                
  142.                 game.ReplicatedStorage.PetSystemRE:FireClient(plr, randomPet)
  143.                
  144.                 randomPet:Clone().Parent = plr.OwnedPets
  145.             end
  146.         end)
  147.     end
  148. end
  149.  
  150.  
  151. game.ReplicatedStorage.PetSystemRE.OnServerEvent:Connect(function(plr, petToEquip)
  152.    
  153.     if plr.Character and petToEquip.Parent == plr.OwnedPets then
  154.        
  155.         local createNewPet = true
  156.        
  157.         if plr.Character:FindFirstChild("EquippedPet") then
  158.            
  159.             if plr.Character.EquippedPet.Decal.Texture == petToEquip.Decal.Texture and plr.Character.EquippedPet.Color == petToEquip.Color then
  160.                
  161.                 createNewPet = false
  162.             end
  163.                
  164.             plr.Character.EquippedPet:Destroy()
  165.         end
  166.        
  167.         if createNewPet then
  168.            
  169.             local newPet = petToEquip:Clone()
  170.             newPet.Name = "EquippedPet"
  171.            
  172.             newPet.CFrame = plr.Character.HumanoidRootPart.CFrame
  173.  
  174.             newPet.Parent = plr.Character
  175.         end
  176.     end
  177. end)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement