DataStore not saving

So I am making a pet data store its for my sim game and for some reason it doesn’t save it saves but it doesn’t load the data in.

I don’t know why but here’s the datastore script.

local dataStoreService = game:GetService("DataStoreService")
local petsDs = dataStoreService:GetDataStore("petsDs")
game.Players.PlayerAdded:Connect(function(player)
      	local equippedPet = Instance.new("StringValue", player)
	    equippedPet.Name = "EquippedPet"

        	local data = petsDs:GetAsync(player.UserId.."-pet")
	
	if data then
		for i, petName in pairs(data) do
			if game.ReplicatedStorage:WaitForChild("Pets"):FindFirstChild(petName) then
				local stringValue = Instance.new("StringValue")
				stringValue.Name = petName
				stringValue.Name = player.PetInventory
			end
		end
		
		game.ReplicatedStorage.Events.PetEvents.SendPetData:FireClient(player,data)
		print("Sent data to player")
	else
		print("Data not found.")
	end
end)

local function savePets(player)
	if player:FindFirstChild("PetInventory") then
		local inventory = {}

		for i, v in pairs(player.PetInventory:GetChildren()) do
			table.insert(inventory, v.Name)
		end

		local success, errorMessage = pcall(function()
			petsDs:GetAsync(player.UserId.."-pet",inventory)
		end)

		if success then
			print("Player Pets Saved!")
		else
			warn("Pets not saved error: "..errorMessage)
		end
	end

	if player:FindFirstChild("EquippedPet") then
		if player.EquippedPet.Value ~= nil then
			local success, errorMessage = pcall(function()
				petsDs:SetAsync(player.UserId.."-equippedPet", player.EquippedPet.Value)
			end)
		end
	end
end

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

game:BindToClose(function(player)
	savePets(player)
end)

now the event

game.ReplicatedStorage.Events.PetEvents.SendPetData.OnClientEvent:Connect(function(petNames)
	for i, petName in pairs(petNames:GetChildren()) do
		if game.ReplicatedStorage.Pets:FindFirstChild(petName) then
			addToFrame(game.ReplicatedStorage.Pets:FindFirstChild(petName))			
		end
		print("Event done!")
	end
end)
1 Like

Here’s your issue.

local data = petsDs:GetAsync(player.UserId.."-pet")
petsDs:SetAsync(player.UserId.."-equippedPet", player.EquippedPet.Value)
1 Like

I apparently got another error

invalid argument #1 to ‘pairs’ (table expected, got string)

		for i, petName in pairs(data) do
			if game.ReplicatedStorage:WaitForChild("Pets"):FindFirstChild(petName) then
				local stringValue = Instance.new("StringValue")
				stringValue.Name = petName
				stringValue.Name = player.PetInventory
			end
		end
		
		game.ReplicatedStorage.Events.PetEvents.SendPetData:FireClient(player,data)
		print("Sent data to player")
	else
		print("Data not found.")
	end

You’re saving a string not a table.

1 Like

Well, yes. Considering you’re saving a string to the DataStore, it would be good to assume you’d get a string when you load it, and you can’t loop over a string.

oh umm I don’t know what to do I am confused

Where you do InstanceValue.Value convert it to an array.

{InstanceValue.Value}

In that case, you should probably learn about data types before progressing any further.

1 Like
local dataStoreService = game:GetService("DataStoreService")
local petsDs = dataStoreService:GetDataStore("petsDs")
game.Players.PlayerAdded:Connect(function(player)
      	local equippedPet = Instance.new("StringValue", player)
	    equippedPet.Name = "EquippedPet"

        	local data = petsDs:GetAsync(player.UserId.."-pet")
	
	if data then
		for i, petName in ipairs(data) do
			if game.ReplicatedStorage:WaitForChild("Pets"):FindFirstChild(petName) then
				local stringValue = Instance.new("StringValue")
				stringValue.Name = petName
				stringValue.Name = player.PetInventory
			end
		end
		
		game.ReplicatedStorage.Events.PetEvents.SendPetData:FireClient(player,data)
		print("Sent data to player")
	else
		print("Data not found.")
	end
end)

local function savePets(player)
	if player:FindFirstChild("PetInventory") then
		local inventory = {}

		for i, v in pairs(player.PetInventory:GetChildren()) do
			table.insert(inventory, v.Name)
		end

		local success, errorMessage = pcall(function()
			petsDs:GetAsync(player.UserId.."-pet",inventory)
		end)

		if success then
			print("Player Pets Saved!")
		else
			warn("Pets not saved error: "..errorMessage)
		end
	end

	if player:FindFirstChild("EquippedPet") then
		if player.EquippedPet.Value ~= nil then
			local success, errorMessage = pcall(function()
				petsDs:SetAsync(player.UserId.."-pet", {player.EquippedPet.Value})
			end)
		end
	end
end

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

game:BindToClose(function(player)
	savePets(player)
end)

I don’t see that you save data with ‘-pet’ key.

on the event I got this: attempt to call a nil value
and
attempt to index nil with ‘FindFirstChild’ with player:FindFirstChild(“PetInventory”)

Event:

game.ReplicatedStorage.Events.PetEvents.SendPetData.OnClientEvent:Connect(function(data)
local pet_name = game.ReplicatedStorage.Pets[data]	
    if pet_name then
		addToFrame(pet_name)			
	end
end)

It should save equipped pet.

attempt to index nil with ‘FindFirstChild’