Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- local DataStoreService = game:GetService("DataStoreService")
- local PetDataStore = DataStoreService:GetDataStore("PetDataStore")
- local function equipPet (player,pet)
- local character = player.Character
- if pet ~= nil and character ~= nil then
- if character:FindFirstChild(player.Name.."'s Pet") then character[player.Name.."'s Pet"]:Destroy() end
- if character.HumanoidRootPart:FindFirstChild("attachmentCharacter") then
- character.HumanoidRootPart:FindFirstChild("attachmentCharacter"):Destroy()end
- pet.Name = player.Name.. "'s Pet"
- pet:SetPrimaryPartCFrame(character.HumanoidRootPart.CFrame)
- local modelSize = pet.PrimaryPart.Size
- local attachmentCharacter = Instance.new("Attachment")
- attachmentCharacter.Visible = false
- attachmentCharacter.Name = "attachmentCharacter"
- attachmentCharacter.Parent = character.HumanoidRootPart
- attachmentCharacter.Position = Vector3.new(1,1,0) + modelSize
- local AttachmentPet = Instance.new("Attachment")
- AttachmentPet.Visible = false
- AttachmentPet.Parent = pet.PrimaryPart
- local alignPosition = Instance.new("AlignPosition")
- alignPosition.MaxForce = 25000
- alignPosition.Attachment0 = AttachmentPet
- alignPosition.Attachment1 = attachmentCharacter
- alignPosition.Responsiveness = 20
- alignPosition.Parent = pet
- local AlignOrientation = Instance.new("AlignOrientation")
- AlignOrientation.MaxTorque = 25000
- AlignOrientation.Attachment0 = AttachmentPet
- AlignOrientation.Attachment1 = attachmentCharacter
- AlignOrientation.Responsiveness = 25
- AlignOrientation.Parent = pet
- pet.Parent = character
- end
- end
- game.Players.PlayerAdded:Connect(function(player)
- local leaderstats = Instance.new("Folder")
- leaderstats.Name = "leaderstats"
- leaderstats.Parent = player
- local Coins = Instance.new("IntValue")
- Coins.Name = "Coins"
- Coins.Value = 10000
- Coins.Parent = leaderstats
- local XP = Instance.new("IntValue")
- XP.Name = "XP"
- XP.Value = 10000
- XP.Parent = leaderstats
- local pets = Instance.new("IntValue")
- pets.Name = "Pets"
- pets.Parent = player
- local inventory = Instance.new("Folder")
- inventory.Name = "PetInventory"
- inventory.Parent = player
- local equippedPet = Instance.new("StringValue")
- equippedPet.Name = "EquippedPet"
- equippedPet.Parent = player
- player.CharacterAdded:Connect(function(char)
- if game.ReplicatedStorage:WaitForChild("Pets"):FindFirstChild(equippedPet.Value)then
- equipPet(player, game.ReplicatedStorage:WaitForChild("Pets"):FindFirstChild(equippedPet.Value):Clone())
- end
- end)
- equippedPet.Changed:Connect(function()
- if equippedPet.Value ~=nil then
- if game.ReplicatedStorage:WaitForChild("Pets"):FindFirstChild(equippedPet.Value)then
- equipPet(player, game.ReplicatedStorage:WaitForChild("Pets"):FindFirstChild(equippedPet.Value):Clone())
- end
- end
- end)
- local data = PetDataStore:GetAsync(player.UserId.."-pet")
- if data then
- for i, petName in pairs(data) do
- if game.ReplicatedStorage:WaitForChild("Pets"):FindFirstChild(petName) then
- local stringValue = Instance.new("StringValue")
- stringValue.Name = petName
- stringValue.Parent = player.PetInventory
- end
- end
- game.ReplicatedStorage.SendData:FireClient(player,data)
- print("Fired sendData!")
- else
- print("No data found, must be a new player or something happened to the data")
- end
- local equippedPetData = PetDataStore:GetAsync(player.UserId.."-equippedPet")
- if equippedPetData then
- equippedPet.Value = equippedPetData
- end
- game.ReplicatedStorage.SetEquippedPet:FireClient(player,equippedPetData)
- end)
- local function saveData(player)
- if player:FindFirstChild("PetInventory") then
- local inventory = {}
- for i,v in pairs(player.PetInventory:GetChildren()) do
- table.insert(inventory,v.Name)
- end
- local success, errorMessage = pcall (function()
- PetDataStore:SetAsync(player.UserId.."-pet",inventory)
- end)
- if success then
- print("Data Saved")
- else
- print("Error: ".. errorMessage)
- end
- end
- if player:FindFirstChild("EquippedPet") then
- if player.EquippedPet.Value ~= nil then
- local success, errorMessage = pcall(function()
- PetDataStore:SetAsync(player.UserId.."-equippedPet",player.EquippedPet.Value)
- end)
- end
- end
- end
- game.Players.PlayerRemoving:Connect(function(player)
- saveData(player)
- end)
- game:BindToClose(function()
- for i, player in pairs(game.Players:GetPlayers()) do
- saveData(player)
- end
- end)
- game.ReplicatedStorage.EquipPet.OnServerEvent:Connect(function(player,petName)
- local pet = game.ReplicatedStorage.Pets:FindFirstChild(petName)
- if pet and player.PetInventory:FindFirstChild(petName) then
- player.EquippedPet.Value = petName
- end
- end)
- game.ReplicatedStorage.UnEquipPet.OnServerEvent:Connect(function(player)
- player.EquippedPet.Value = ""
- if player.Character:FindFirstChild(player.Name.."'s Pet") then
- player.Character[player.Name.."'s Pet"]:Destroy()
- end
- if player.Character.HumanoidRootPart:FindFirstChild("attachmentCharacter") then
- player.Character.HumanoidRootPart:FindFirstChild("attachmentCharacter"):Destroy()
- end
- end)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement