Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- --Variables
- local dss = game:GetService("DataStoreService")
- local ds = dss:GetDataStore("PETS DATASTORE")
- local rs = game:GetService("ReplicatedStorage")
- local pets = rs:WaitForChild("Pets")
- local remotes = rs:WaitForChild("RemoteEvents")
- local rnd = Random.new()
- local config = require(rs:WaitForChild("CONFIGURATION"))
- local add = require(script:WaitForChild("AddPetToInventory"))
- local del = require(script:WaitForChild("DeletePetFromInventory"))
- local equip = require(script:WaitForChild("EquipPet"))
- local unequip = require(script:WaitForChild("UnequipPet"))
- local create = require(script:WaitForChild("CreatePet"))
- --Saving and Loading Data
- function saveData(plr:Player)
- if not plr:FindFirstChild("DATA FAILED TO LOAD") then
- local plrCash = plr.leaderstats.Cash.Value
- local plrInventory = {}
- for _, pet in pairs(plr.PetsInventory:GetChildren()) do
- table.insert(plrInventory, pet.Value.Name)
- end
- local plrEquipped = {}
- for _, pet in pairs(plr.PetsEquipped:GetChildren()) do
- if pet.Value then
- table.insert(plrEquipped, pet.Value.Name)
- end
- end
- local compiledData = {}
- compiledData.Cash = plrCash
- compiledData.Inventory = plrInventory
- compiledData.Equipped = plrEquipped
- local success, err = nil, nil
- while not success do
- success, err = pcall(function()
- ds:SetAsync(plr.UserId, compiledData)
- end)
- if err then
- warn(err)
- end
- task.wait(0.02)
- end
- end
- end
- game.Players.PlayerRemoving:Connect(saveData)
- game:BindToClose(function()
- for _, plr in pairs(game.Players:GetPlayers()) do
- saveData(plr)
- end
- end)
- game.Players.PlayerAdded:Connect(function(plr)
- plr.CharacterAdded:Connect(function(char)
- if not plr:FindFirstChild("DATA FAILED TO LOAD") then
- local equippedPetsContainer = Instance.new("Folder")
- equippedPetsContainer.Name = "EQUIPPED PETS"
- equippedPetsContainer.Parent = char
- for _, equippedValue in pairs(plr:WaitForChild("PetsEquipped"):GetChildren()) do
- if equippedValue.Value then
- create(plr, equippedValue)
- end
- end
- end
- end)
- local dataFailedWarning = Instance.new("StringValue")
- dataFailedWarning.Name = "DATA FAILED TO LOAD"
- local success, plrData = nil, nil
- while true do
- success, plrData = pcall(function()
- return ds:GetAsync(plr.UserId)
- end)
- task.wait(0.02)
- if not success then
- dataFailedWarning.Parent = plr
- else
- break
- end
- end
- dataFailedWarning:Destroy()
- if not plrData then
- plrData = {
- Cash = 30000;
- Inventory = {"Dog"};
- Equipped = {};
- }
- end
- local ls = Instance.new("Folder")
- ls.Name = "leaderstats"
- ls.Parent = plr
- local cashValue = Instance.new("IntValue")
- cashValue.Name = "Cash"
- cashValue.Value = plrData.Cash
- cashValue.Parent = ls
- local inventory = Instance.new("Folder")
- inventory.Name = "PetsInventory"
- inventory.Parent = plr
- local equipped = Instance.new("Folder")
- equipped.Name = "PetsEquipped"
- equipped.Parent = plr
- for _, pet in pairs(plrData.Inventory) do
- local foundPet = pets:FindFirstChild(pet, true)
- if foundPet and foundPet.Parent.Parent == pets then
- local petValue = Instance.new("ObjectValue")
- petValue.Value = foundPet
- petValue.Parent = inventory
- end
- end
- for i = 1, config.MaxPetsEquipped do
- local equippedValue = Instance.new("ObjectValue")
- equippedValue.Name = i
- local linkedPet = Instance.new("ObjectValue")
- linkedPet.Name = "LINKED PET"
- linkedPet.Parent = equippedValue
- equippedValue:GetPropertyChangedSignal("Value"):Connect(function()
- local newValue = equippedValue.Value
- if linkedPet.Value then
- linkedPet.Value:Destroy()
- end
- if newValue then
- create(plr, equippedValue)
- end
- end)
- if plrData.Equipped[i] then
- local foundPet = pets:FindFirstChild(plrData.Equipped[i], true)
- if foundPet and foundPet.Parent.Parent == pets then
- equippedValue.Value = foundPet
- end
- end
- equippedValue.Parent = equipped
- end
- end)
- --Deleting Pets
- remotes:WaitForChild("DeletePet").OnServerEvent:Connect(del)
- --Equipping Pets
- remotes:WaitForChild("EquipPet").OnServerEvent:Connect(equip)
- --Unequipping Pets
- remotes:WaitForChild("UnequipPet").OnServerEvent:Connect(unequip)
- --Setting up Incubators
- for _, incubator in pairs(workspace:WaitForChild("Incubators"):GetChildren()) do
- local prompt = Instance.new("ProximityPrompt")
- prompt.ObjectText = incubator.Name
- prompt.ActionText = "View pets"
- prompt.HoldDuration = 0
- prompt.KeyboardKeyCode = Enum.KeyCode.F
- prompt.RequiresLineOfSight = false
- prompt.MaxActivationDistance = 7
- prompt.Parent = incubator.PrimaryPart or incubator
- prompt.Triggered:Connect(function(plr)
- remotes:WaitForChild("ViewIncubator"):FireClient(plr, incubator)
- end)
- end
- --Hatching Pets
- remotes:WaitForChild("HatchPet").OnServerEvent:Connect(function(plr, incubator)
- if plr and incubator and incubator.Parent == workspace.Incubators then
- local incubatorConfig = require(incubator.Configuration)
- if plr.leaderstats.Cash.Value >= incubatorConfig.Price and #plr.PetsInventory:GetChildren() < config.MaxPetsInventory then
- plr.leaderstats.Cash.Value -= incubatorConfig.Price
- local chances = incubatorConfig.Chances
- local plrChance = rnd:NextNumber() * 100
- local n = 0
- local rarityChosen = nil
- for rarity, chance in pairs(chances) do
- n += chance
- if plrChance <= n then
- rarityChosen = rarity
- break
- end
- end
- local hatchablePets = incubatorConfig.HatchablePets
- for i = #hatchablePets, 2, -1 do
- local j = rnd:NextInteger(1, i)
- hatchablePets[i], hatchablePets[j] = hatchablePets[j], hatchablePets[i]
- end
- local petChosen = nil
- for _, petName in pairs(hatchablePets) do
- if pets:FindFirstChild(petName, true) and pets:FindFirstChild(petName, true).Parent.Name == rarityChosen then
- petChosen = petName
- break
- end
- end
- add(plr, petChosen)
- remotes:WaitForChild("HatchPet"):FireClient(plr, petChosen)
- end
- end
- end)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement