I’m currently making a pet system, and the inventory folder is turned into a table and then saved. However, I’ve noticed that it doesn’t save multiple things of the same name.
For example:
Gets 2 bats (which doesn’t save)
But this saves:
Gets 1 dragon & 1 bat (which saves as they are different)
So, is this even possible? If not, how can I work around it? And if it is possible, where am I going wrong? I can provide a script if it’s actually possible, but for now, I’m fairly sceptical on whether it is.
If you have each stat saved as a ‘slot’ (such as Slot1, Slot2, Slot3 etc etc), you can just make it’s value the name of the pet. I don’t see why that wouldn’t work.
@mistr88 Can you explain how that would work in more detail?
@K_reex That sounds simple to do. What would be the easiest way of doing this?
--// Saving
PetInventory[v.Name] = v.Name
PetInventory[v.ID] = v:WaitForChild("ID").Value
--// Loading
for i,v in pairs(PetData) do
print(v)
local Target = Pets:FindFirstChild(v)
if Target then
local Clone = Target:Clone()
Clone.Parent = PetInventory
local PetID = Instance.new("IntValue")
PetID.Name = "ID"
PetID.Parent = Clone
PetID.Value = v.ID
end
end
^ Snippets only, this is not the whole script, just relevant parts to this question
So, what I’m thinking is:
Pet is bought, added to player’s “pet inventory” folder in player’s children
IntValue in pet’s children is set to player’s OwnedPet’s value (unique ID)
OwnedPet’s value increases by 1 for the next pet
When player leaves, ID in table is assigned like this: PetInventory[v.ID] = v:WaitForChild("ID").Value
When player joins, get the ID from the table where the name is, and assign it to the pet’s IntValue’s value.
Would this process work, and would the script that I showed at the top ^^ work?
I believe that would work. Try implementing it and report the results! The important thing is that you’re saving / loading the pet by unique ID, and not the name.
I’m not entirely sure how your specific pet system works, but:
In the case that the pet is an instance with the following values instances within: string Pet_Type … Type of pet (“Dog”, “Cat”, “Bunny”) – this should be prebuilt into the pet model int Pet_ID … Unique ID of the pet string Given_Name … Whatever the player named their pet (“Bobby”, “Duke”, “Vladmir”)
We want to store the pet with Pet_ID, not Pet_Type or Given_Name because those can be the same for multiple pets.
-- In a server script
local Players = game:GetService("Players")
local DataStore = game:GetService("DataStoreService"):GetDataStore("pet_datastore")
local PetStorage = game.ServerStorage.PetStorage -- where all the pet models are kept
-- Saving
Players.PlayerRemoving:Connect(function(Player)
local Inventory = Player.Pet_Inventory
PetData = {}
for _, PetObject in pairs(Inventory:GetChildren()) do
PetData[PetObject.Pet_ID.Value] = {
Pet_Type = PetObject.Pet_Type.Value,
Given_Name = PetObject.Given_Name.Value
}
end
DataStore:SetAsync(Player.UserId, PetData)
end)
-- Loading
Players.PlayerAdded:Connect(function(Player)
local Inventory = Instance.new("Folder")
Inventory.Name = "Pet_Inventory"
Inventory.Parent = Player
local PetData = DataStore:GetAsync(Player.UserId)
if PetData then
for i,v in pairs(PetData) do
local PetObject = PetStorage[v.Pet_Type]:Clone()
PetObject.Pet_ID.Value = i
PetObject.Given_Name.Value = v.Given_Name
end
else -- Player has never played the game before, and doesn't have any pet data
DataStore:SetAsync(Player.UserId, {})
end
end)