Sungmingamerpro13

PetDataStore for Story Games

Jun 12th, 2026
117
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
CSS 1.21 KB | None | 0 0
  1. local TeleportService = game:GetService("TeleportService")
  2. local DataStoreService = game:GetService("DataStoreService")
  3. local PetStore = DataStoreService:GetDataStore("PetData")
  4.  
  5. game.Players.PlayerAdded:Connect(function(Player)
  6.  
  7.     local OwnedPets = Instance.new("Folder")
  8.     OwnedPets.Name = "OwnedPets"
  9.     OwnedPets.Parent = Player
  10.  
  11.     local EquippedPet = Instance.new("StringValue")
  12.     EquippedPet.Name = "EquippedPet"
  13.     EquippedPet.Value = ""
  14.     EquippedPet.Parent = Player
  15.  
  16.     local Success, Data = pcall(function()
  17.         return PetStore:GetAsync(Player.UserId)
  18.     end)
  19.  
  20.     if Success and Data then
  21.  
  22.         if Data.EquippedPet then
  23.             EquippedPet.Value = Data.EquippedPet
  24.         end
  25.  
  26.         if Data.OwnedPets then
  27.  
  28.             for PetName, HasPet in pairs(Data.OwnedPets) do
  29.  
  30.                 if HasPet then
  31.  
  32.                     local Pet = Instance.new("BoolValue")
  33.                     Pet.Name = PetName
  34.                     Pet.Parent = OwnedPets
  35.  
  36.                 end
  37.  
  38.             end
  39.  
  40.         end
  41.  
  42.     end
  43.  
  44. end)
  45.  
  46. game.Players.PlayerRemoving:Connect(function(Player)
  47.  
  48.     local Pets = {}
  49.  
  50.     for _, Pet in ipairs(Player.OwnedPets:GetChildren()) do
  51.         Pets[Pet.Name] = true
  52.     end
  53.  
  54.     pcall(function()
  55.  
  56.         PetStore:SetAsync(Player.UserId,{
  57.             OwnedPets = Pets,
  58.             EquippedPet = Player.EquippedPet.Value
  59.         })
  60.  
  61.     end)
  62.  
  63. end)
Advertisement
Add Comment
Please, Sign In to add comment