DataStore not getting or saving array correctly

I’m trying to make a sort of RTS DataStore where Units are stored. Upon Getting the datastore value, it always returns an empty array. I added a couple of print statements and proved that the array returned from the datastore is empty. I would love some help or insight into this
Code:

local DataStore = game:GetService("DataStoreService")
local UnitStore3 = DataStore:GetDataStore("UnitDataStore")
game.Players.PlayerAdded:Connect(function(plr)
	repeat wait() until plr.Character
	local OwnedUnits = Instance.new("Folder",workspace)
	OwnedUnits.Name = plr.Name.."_Units"
	local Units
	local success, eror = pcall(function()
		local UUnits =  UnitStore3:GetAsync("OwnedUnits_"..plr.Name)
		Units = game:GetService("HttpService"):JSONDecode(UUnits)
		print(#Units)
	end)
	if success then
		if #Units  ~= 0 then
			for k,v in pairs(Units) do
				local NewUnit = game.ReplicatedStorage.Units:FindFirstChild(v.Name)
				NewUnit = NewUnit:Clone()
				NewUnit.Parent = OwnedUnits
				
				NewUnit:MoveTo((plr.Character.HumanoidRootPart.CFrame * CFrame.new(0,0,2).Position))
			end
		else
			local NewUnit = game.ReplicatedStorage.Units.Swordsman:Clone()
			NewUnit.Parent = OwnedUnits
			NewUnit:MoveTo((plr.Character.HumanoidRootPart.CFrame * CFrame.new(0,0,2).Position))
			local SaveUnits = {}
			for k,v in pairs(OwnedUnits:GetChildren()) do
				table.insert(SaveUnits,v)
				print(v.Name)
			end
			
			local Encoded = game:GetService("HttpService"):JSONEncode(SaveUnits)
			UnitStore3:SetAsync("OwnedUnits_"..plr.Name, Encoded)

		end
	elseif eror then
		local SaveUnits = {}
			for k,v in pairs(OwnedUnits:GetChildren()) do
			table.insert(SaveUnits,v.Name)
			print(v.Name)
			end
		local NewUnit = game.ReplicatedStorage.Units.Swordsman:Clone()
		NewUnit.Parent = OwnedUnits
		NewUnit:MoveTo((plr.Character.HumanoidRootPart.CFrame * CFrame.new(0,0,2).Position))
		UnitStore3:SetAsync("OwnedUnits_"..plr.Name,game:GetService("HttpService"):JSONEncode(SaveUnits:GetChildren()))
	end
	
	
end)

game.Players.PlayerRemoving:Connect(function(plr)
	local OwnedUnits = workspace:FindFirstChild(plr.Name.."_Units"):GetChildren()
	local SaveUnits = {}
	for k,v in pairs(OwnedUnits:GetChildren()) do
		table.insert(SaveUnits,v)
	end
	UnitStore3:SetAsync("OwnedUnits_"..plr.Name,game:GetService("HttpService"):JSONEncode(SaveUnits:GetChildren()))
end)

Do not use HTTP service to get Datastores values, instead of:

nits =  game:GetService("HttpService"):JSONDecode( UnitStore2:GetAsync("OwnedUnits_"..plr.Name))

It should be:

UnitStore2:GetAsync("OwnedUnits_"..plr.Name)

Still wrapped in the Protected Call of course.

1 Like

I have tried your solution, and it did not fix the problem.

Theres a couple things you should double check

  1. When reading from the DataStore, print out UnitStore3… is it valid?
  2. add a print statement after GetAsync… is the UUnits value valid? I know you added a print statement after the JSON decode, but add one before the JSON decode. If the data from GetAsync is blank…maybe you are saving the data wrong.

Lets double check that you are saving the data correctly.

In the PlayerRemoving code, add a print statement at the start of the function, to show that the code is actually running when you save it… and add a print statement at the end of the function to show that the code function FINISHED.

If you are in Roblox Studio (PlaySolo) and hit STOP, the event “PlayerRemoving” can be fire…but in my experience, sometimes the code halts midway and doesn’t finish. So maybe your SetAsync is not being called.

Also in your SetAsync function, split that one line into two lines… Decode JSON first… print out the value, then perform the SetAsync…

Then just for testing, right after the SetAsync …perform a GetAsync and read the value…make sure its correct.

1 Like