I’ve made a pet inventory system that saves the pet’s level and food amount. What can I do to make it run or/and look better?
local ds = game:GetService("DataStoreService"):GetDataStore("Backpack")
local backpack_folder = game.ServerStorage.Backpack--Define the backpack in ServerStorage
local pets_folder = game.ServerStorage.Backpack.Pets--Define the folder in ServerStorage with the pets
local ShowPet = game.ReplicatedStorage:WaitForChild("PurchasePrompts").ShowPet--Define the remote event that shows the pet in the playert's backpack
game.Players.PlayerAdded:Connect(function(plr)
local Backpack = Instance.new("Folder")--Create a folder for their backpack
Backpack.Name = "Backpack"
Backpack.Parent = plr
for _, v in pairs (backpack_folder:GetChildren()) do--Loop through all of the folders in the backpack
local sub_folder = Instance.new("Folder")--Create a subFolder to be a child of the backpack (ex. Pets)
sub_folder.Name = v.Name -- Name it the folder in ServerStorage
sub_folder.Parent = Backpack
end
local petInventory = ds:GetAsync("PetInventory-"..plr.UserId)--get the player's pet inv data
if petInventory then --If they have data
for _, pet in pairs(petInventory) do--loop through all of the pets the player has
if game.ServerStorage.Backpack.Pets:FindFirstChild(pet[1]) then--if the pet is a pet in ServerStorage
local succ, msg = pcall(function()
local petValue = Instance.new("Folder")--create a folder for the pet and put it in the player's backpack
petValue.Name = pet[1]
petValue.Parent = Backpack:FindFirstChild("Pets")
local petLevel = pet[2]--get the pet's level and put it under the pet
local AddPetLevel = Instance.new("IntValue")
AddPetLevel.Name = "Level"
AddPetLevel.Parent = petValue
AddPetLevel.Value = petLevel
local petFood = pet[3]--get the pet's food and put it under the pet
local AddPetFood = Instance.new("IntValue")
AddPetFood.Name = "Food"
AddPetFood.Parent = petValue
AddPetFood.Value = petFood
ShowPet:FireClient(plr, pet[1])--fire the player so the pet shows up in their inventory
end)
if not succ then
warn("Problem with getting and setting data ".. msg)
end
end
end
end
end)
game.Players.PlayerRemoving:Connect(function(plr)
local succ, msg = pcall(function()
local petTable = {}--creates a table
for i, pet in pairs(plr.Backpack.Pets:GetChildren()) do
table.insert(petTable, {pet.Name, pet.Level.Value, pet.Food.Value})--insert pet table {pet's name, pet's level, pet's food} ex. {"Ocean Egg", 6, 5}
end
ds:SetAsync("PetInventory-"..plr.UserId, petTable)--saving the table to the player
end)
if not succ then
warn("Problem with saving data ".. msg)
end
end)
Thanks!