Problem with Saving Halos using DataStore

Hello Everyone! I’ve made a Halo Shop but I wanted it to saving. And I’ve made I guess some basic DataStore. And it saves everything but not the Halos. I have an error saying ServerScriptService.Server.DataStores.MainDataStore:30: invalid argument #3 (Instance expected, got table) - Server - MainDataStore:30

Here is ServerScript where is error:

local players = game:GetService("Players")
local ds = game:GetService("DataStoreService")
local statsDS = ds:GetDataStore("dataStoreStats")

players.PlayerAdded:Connect(function(player)
	wait(1)
	local userId = player.UserId
	local stat1 = player.leaderstats["Coins"]
	local stat2 = player.leaderstats["Treasures Found"]
	local stat3 = player.leaderstats["City Opened"]
	local stat4 = player.leaderstats["Factory Opened"]
	
	local ownedHalosData
	local equippedStat = player.Data["EquippedHalo"]
	
	local getSaved = statsDS:GetAsync(userId) 
	if getSaved then
		stat1.Value = getSaved[1]
		stat2.Value = getSaved[2]
		stat3.Value = getSaved[3]
		stat4.Value = getSaved[4]
	end
	local succ, err = pcall(function()
		ownedHalosData = statsDS:GetAsync(userId.."-OwnedHalos")
	end)
	if succ then
		if ownedHalosData then
			for i,halo in pairs(ownedHalosData) do
				local insert = Instance.new("StringValue")
				insert.Parent = ownedHalosData
				insert.Name = halo
			end
		end
	end
end)
players.PlayerRemoving:Connect(function(player)
	local userId = player.UserId
	local data = {
		player.leaderstats["Coins"].Value,
		player.leaderstats["Treasures Found"].Value,
		player.leaderstats["City Opened"].Value,
		player.leaderstats["Factory Opened"].Value,
		player.Data["EquippedHalo"].Value
	};
	local ownedHalosData = {}
	local success, result = pcall(function()
		for i,v in pairs(player.Data:FindFirstChild("OwnedHalos"):GetChildren()) do
			table.insert(ownedHalosData,v.Name)
		end
		statsDS:SetAsync(userId.."-OwnedHalos", ownedHalosData)
		statsDS:SetAsync(userId, data)
	end)
	if success then print("StatsData successfuly saved!")
	else warn(result) end
end)

I don’t know what problem is here, so that why I’m here. If someone can help, that would be really appreciated! Thanks.

insert.Parent = ownedHalosData
It’s pretty obvious what the problem is here.

But I don’t know how to fix it.

Create a new Folder and then set the insert parent to the Folder.

	local succ, err = pcall(function()
		ownedHalosData = statsDS:GetAsync(userId.."-OwnedHalos")
	end)
	if succ and ownedHalosData then
		local HalosOwned = Instance.new("Folder", player.Data)
		HalosOwned.Name = "HalosOwned"
		for i,halo in pairs(ownedHalosData) do
			local insert = Instance.new("StringValue", HalosOwned)
			insert.Name = halo
		end
	end

I already have folder in other script
image

Then just set the Parent as that folder. The issue at hand is that you are setting the Parent to a table.

Thanks! Now it’s working and saving perfectly.

1 Like

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