DataSave unable to save a StringValue after its name was changed

So basically whenever I change a stringvalue’s name and rejoin, that stringvalue will not exist.
image
For example “Jack Rabbit2” will not be in my inventory after I rejoin.

Setting the name serverscript inside serverscriptservice:

local namenumber = player.Stats.NameNumber
for i, petInIventory in pairs(player.PetInventory:GetChildren()) do
	if petInIventory.Name == petVal.Name then
		namenumber.Value += 1
		petVal.Name = pet.Name..namenumber.Value
	end
end

Saving the pet serverscript inside serverscriptservice:

game.Players.PlayerAdded:Connect(function(player)
	
	local inventory = PetDataStore:GetAsync(player.UserId.."pet")
	local uuid = PetDataStore:GetAsync(player.UserId.."uuid")
	local equipped = PetDataStore:GetAsync(player.UserId.."equipped")

	if inventory and uuid then

		for i, petName in pairs(inventory) do
			if game.ReplicatedStorage:WaitForChild("Pets"):FindFirstChild(petName) then
				local stringValue = Instance.new("StringValue")
				stringValue.Name = petName
				stringValue.Parent = player.PetInventory
				stringValue.Value = uuid[i]
				wait(0.03)
				local boolValue = Instance.new("BoolValue")
				boolValue.Name = "Equipped"
				boolValue.Parent = stringValue
				boolValue.Value = equipped[i]
			end
		end

		game.ReplicatedStorage:WaitForChild("PetRE").AddToFrame:FireClient(player,inventory)
		
	end
end)

local function saveInventory(player)
	if player:FindFirstChild("PetInventory") then
		local inventory = {}
		local uuid = {}
		local equipped = {}

		for i, pet in pairs(player.PetInventory:GetChildren()) do
			table.insert(inventory,pet.Name)
			table.insert(uuid,pet.Value)
			table.insert(equipped,pet.Equipped.Value)
		end

		local success, errorMessage = pcall(function()
			PetDataStore:SetAsync(player.UserId.."pet",inventory)
			PetDataStore:SetAsync(player.UserId.."uuid",uuid)
			PetDataStore:SetAsync(player.UserId.."equipped",equipped)
		end)

		if success then
			print("Data saved")
		else
			print("Error: "..errorMessage)
		end
	end
end

game.Players.PlayerRemoving:Connect(function(player)
	saveInventory(player)
end)

game:BindToClose(function()
	for i, player in pairs(game.Players:GetPlayers()) do
		saveInventory(player)
	end
end)

I have found the solution for this problem.

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.