Saving a Name Table

Save Script:

local PetsTable = {}

for i, pet in pairs(player.Pets:GetChildren()) do
	table.insert(PetsTable, pet.Name)
end

local Psuccess, Perr = pcall(function()
	PetDataStore:SetAsync(player.UserId, PetsTable)
end)

Load Script:

local Pdata
local Psuccess, Perr = pcall(function()
	Pdata = DataStore:GetAsync(player.UserId)
end)
	
if Psuccess and Pdata then
	for i, pet in pairs(Pdata) do
		local PetValue = Instance.new("NumberValue")
		PetValue.Name = pet
		PetValue.Value = Pets:FindFirstChild(pet, true):GetAttribute("Multiplier")
		PetValue.Parent = player.Pets
	end
end

When I print ‘pet.Name’ in the Save Script after the ‘table.insert’ line, it prints the pet’s name. Though, when I print ‘pet’ in the Load Script after the ‘for i, pet in pairs(Pdata) do’ line, it prints ‘0’. I’m not sure why.

Any help is appreciated! :smile:

Why are you saving the pet’s name and putting it in numbervalue, isn’t it a string?

1 Like

I’m setting the name (string) of the NumberValue to the pet’s name.

I couldn’t quite understand what you mean because my English is a bit bad, but can you try to write this in the load script and try it?

local Pdata
local Psuccess, Perr = pcall(function()
	Pdata = DataStore:GetAsync(player.UserId)
end)
	
if Psuccess and Pdata then
	for i, pet in pairs(Pdata) do
		local PetValue = Instance.new("StringValue")
		PetValue.Name = pet
		PetValue.Value = Pets:FindFirstChild(pet, true):GetAttribute("Multiplier")
		PetValue.Parent = player.Pets
	end
end