Advertisement
Jackthehunter25

Untitled

Jan 25th, 2021
166
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.19 KB | None | 0 0
  1. local DataStoreService = game:GetService("DataStoreService")
  2.  
  3. local PetDataStore = DataStoreService:GetDataStore("PetDataStore")
  4.  
  5.  
  6. local function equipPet (player,pet)
  7.  
  8. local character = player.Character
  9.  
  10. if pet ~= nil and character ~= nil then
  11.  
  12. if character:FindFirstChild(player.Name.."'s Pet") then character[player.Name.."'s Pet"]:Destroy() end
  13. if character.HumanoidRootPart:FindFirstChild("attachmentCharacter") then
  14. character.HumanoidRootPart:FindFirstChild("attachmentCharacter"):Destroy()end
  15.  
  16.  
  17. pet.Name = player.Name.. "'s Pet"
  18.  
  19. pet:SetPrimaryPartCFrame(character.HumanoidRootPart.CFrame)
  20.  
  21. local modelSize = pet.PrimaryPart.Size
  22.  
  23. local attachmentCharacter = Instance.new("Attachment")
  24. attachmentCharacter.Visible = false
  25. attachmentCharacter.Name = "attachmentCharacter"
  26. attachmentCharacter.Parent = character.HumanoidRootPart
  27. attachmentCharacter.Position = Vector3.new(1,1,0) + modelSize
  28.  
  29.  
  30. local AttachmentPet = Instance.new("Attachment")
  31. AttachmentPet.Visible = false
  32. AttachmentPet.Parent = pet.PrimaryPart
  33.  
  34. local alignPosition = Instance.new("AlignPosition")
  35. alignPosition.MaxForce = 25000
  36. alignPosition.Attachment0 = AttachmentPet
  37. alignPosition.Attachment1 = attachmentCharacter
  38. alignPosition.Responsiveness = 20
  39. alignPosition.Parent = pet
  40.  
  41. local AlignOrientation = Instance.new("AlignOrientation")
  42. AlignOrientation.MaxTorque = 25000
  43. AlignOrientation.Attachment0 = AttachmentPet
  44. AlignOrientation.Attachment1 = attachmentCharacter
  45. AlignOrientation.Responsiveness = 25
  46. AlignOrientation.Parent = pet
  47.  
  48.  
  49.  
  50.  
  51.  
  52.  
  53.  
  54. pet.Parent = character
  55.  
  56. end
  57. end
  58.  
  59.  
  60.  
  61. game.Players.PlayerAdded:Connect(function(player)
  62. local leaderstats = Instance.new("Folder")
  63. leaderstats.Name = "leaderstats"
  64. leaderstats.Parent = player
  65.  
  66. local Coins = Instance.new("IntValue")
  67. Coins.Name = "Coins"
  68. Coins.Value = 10000
  69. Coins.Parent = leaderstats
  70.  
  71. local XP = Instance.new("IntValue")
  72. XP.Name = "XP"
  73. XP.Value = 10000
  74. XP.Parent = leaderstats
  75.  
  76. local pets = Instance.new("IntValue")
  77. pets.Name = "Pets"
  78. pets.Parent = player
  79.  
  80.  
  81. local inventory = Instance.new("Folder")
  82. inventory.Name = "PetInventory"
  83. inventory.Parent = player
  84.  
  85. local equippedPet = Instance.new("StringValue")
  86. equippedPet.Name = "EquippedPet"
  87. equippedPet.Parent = player
  88.  
  89. player.CharacterAdded:Connect(function(char)
  90. if game.ReplicatedStorage:WaitForChild("Pets"):FindFirstChild(equippedPet.Value)then
  91. equipPet(player, game.ReplicatedStorage:WaitForChild("Pets"):FindFirstChild(equippedPet.Value):Clone())
  92. end
  93. end)
  94.  
  95. equippedPet.Changed:Connect(function()
  96. if equippedPet.Value ~=nil then
  97. if game.ReplicatedStorage:WaitForChild("Pets"):FindFirstChild(equippedPet.Value)then
  98. equipPet(player, game.ReplicatedStorage:WaitForChild("Pets"):FindFirstChild(equippedPet.Value):Clone())
  99. end
  100. end
  101. end)
  102.  
  103. local data = PetDataStore:GetAsync(player.UserId.."-pet")
  104.  
  105. if data then
  106.  
  107. for i, petName in pairs(data) do
  108. if game.ReplicatedStorage:WaitForChild("Pets"):FindFirstChild(petName) then
  109. local stringValue = Instance.new("StringValue")
  110. stringValue.Name = petName
  111. stringValue.Parent = player.PetInventory
  112. end
  113. end
  114.  
  115. game.ReplicatedStorage.SendData:FireClient(player,data)
  116. print("Fired sendData!")
  117. else
  118. print("No data found, must be a new player or something happened to the data")
  119. end
  120.  
  121. local equippedPetData = PetDataStore:GetAsync(player.UserId.."-equippedPet")
  122.  
  123. if equippedPetData then
  124. equippedPet.Value = equippedPetData
  125. end
  126. game.ReplicatedStorage.SetEquippedPet:FireClient(player,equippedPetData)
  127. end)
  128.  
  129. local function saveData(player)
  130. if player:FindFirstChild("PetInventory") then
  131. local inventory = {}
  132.  
  133. for i,v in pairs(player.PetInventory:GetChildren()) do
  134. table.insert(inventory,v.Name)
  135.  
  136. end
  137.  
  138. local success, errorMessage = pcall (function()
  139. PetDataStore:SetAsync(player.UserId.."-pet",inventory)
  140.  
  141. end)
  142.  
  143. if success then
  144. print("Data Saved")
  145. else
  146. print("Error: ".. errorMessage)
  147. end
  148.  
  149. end
  150.  
  151. if player:FindFirstChild("EquippedPet") then
  152. if player.EquippedPet.Value ~= nil then
  153. local success, errorMessage = pcall(function()
  154. PetDataStore:SetAsync(player.UserId.."-equippedPet",player.EquippedPet.Value)
  155. end)
  156. end
  157. end
  158. end
  159.  
  160. game.Players.PlayerRemoving:Connect(function(player)
  161. saveData(player)
  162. end)
  163.  
  164. game:BindToClose(function()
  165. for i, player in pairs(game.Players:GetPlayers()) do
  166. saveData(player)
  167. end
  168. end)
  169.  
  170.  
  171. game.ReplicatedStorage.EquipPet.OnServerEvent:Connect(function(player,petName)
  172. local pet = game.ReplicatedStorage.Pets:FindFirstChild(petName)
  173.  
  174. if pet and player.PetInventory:FindFirstChild(petName) then
  175. player.EquippedPet.Value = petName
  176.  
  177. end
  178. end)
  179.  
  180. game.ReplicatedStorage.UnEquipPet.OnServerEvent:Connect(function(player)
  181. player.EquippedPet.Value = ""
  182. if player.Character:FindFirstChild(player.Name.."'s Pet") then
  183. player.Character[player.Name.."'s Pet"]:Destroy()
  184. end
  185. if player.Character.HumanoidRootPart:FindFirstChild("attachmentCharacter") then
  186. player.Character.HumanoidRootPart:FindFirstChild("attachmentCharacter"):Destroy()
  187. end
  188. end)
  189.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement