Datastore2 Table Returns Nil

I’m making a script that converts saved maps to models. When I call the datastore for the position that newly instanced baseparts go to (which :FillBlock() is then called on) the game errors:

14:22:11.485 - ServerScriptService.save_script:98: bad argument #3 (CFrame expected, got nil)

I made sure that the function was in the PlayerAdded event which it wasn’t. I changed it and also made it print the value of the terrain position datastore and it said it printed a table and I also printed the 1st value in the table which is nil, so I have a table filled with nil values.

DataStore2 = require(game:GetService("ServerScriptService").DataStore2)
ServerStorage = game:GetService("ServerStorage")
Players = game:GetService("Players")
ReplicatedStorage = game.ReplicatedStorage

default_omega_coins, default_likes, default_wins = 0, 0, 0

default_bunker_name = "Bunker"

default_bunker_terrain_position = {
	CFrame.new(26.927, -33.383, -435.507),
	CFrame.new(26.927, 23.365, -572.788),
	CFrame.new(164.479, 23.365, -434.153),
	CFrame.new(-110.625, 23.365, -435.236),
	CFrame.new(25.844, 23.365, -297.955)
}
default_bunker_terrain_size = {
	Vector3.new(277.27, 8.143, 277.27),
	Vector3.new(277.27, 108.309, 2.708),
	Vector3.new(2.166, 108.309, 274.562),
	Vector3.new(2.166, 108.309, 272.396),
	Vector3.new(275.104, 108.309, 2.166)
}
default_bunker_terrain_rotation = {
	CFrame.Angles(0, 0, 0),
	CFrame.Angles(0, 0, 0),
	CFrame.Angles(0, 0, 0),
	CFrame.Angles(0, 0, 0),
	CFrame.Angles(0, 0, 0)
}
default_bunker_materials = {
	"Mud",
	"Rock",
	"Rock",
	"Rock",
	"Rock"
}

default_object_name, default_object_position, default_object_rotation = {}, {}, {}

DataStore2.Combine("Ham", "Wins", "Ωmega Coins", "Likes", "bunker_name", "object_name", "object_position", "object_rotation", "terrain-position", "terrain_rotation", "terrain_size", "terrain_materials")

Players.PlayerAdded:Connect(function(Player)
	winsStore = DataStore2("Wins", Player)
	coinStore = DataStore2("Ωmega Coins", Player)
	likesStore = DataStore2("Likes", Player)
	
	bunkernameStore = DataStore2("bunker_name", Player)
	objectnameStore = DataStore2("object_name", Player)
	objectpositionStore = DataStore2("object_position", Player)
	objectrotationStore = DataStore2("object_rotation", Player)
	
	terrainpositionStore = DataStore2("terrain-position", Player)
	terrainrotationStore = DataStore2("terrain_rotation", Player)
	terrainsizeStore = DataStore2("terrain_size", Player)
	terrainmaterialsStore = DataStore2("terrain_materials", Player)
	print (terrainpositionStore)
	print(terrainmaterialsStore)
	print(terrainrotationStore)
	
	local new_player = Instance.new("BoolValue", Player)
	new_player.Name = "new_player"
	if winsStore == nil then
		new_player = true
	end
	
	local leaderstats = Instance.new("Folder") do
		leaderstats.Name = "leaderstats"
	end
	local wins = Instance.new("IntValue") do
		wins.Name = "Wins"
		wins.Parent = leaderstats
	end
	local omega_coins = Instance.new("IntValue") do
		omega_coins.Name = "Ωmega $"
		omega_coins.Parent = leaderstats
	end
	local likes = Instance.new("IntValue") do
		likes.Name = "Likes"
		likes.Parent = leaderstats
	end
	leaderstats.Parent = Player
	
	function save_to_model(player, bunker_name) ---------------------------------------------------- function start
	local newMap = ServerStorage.empty_map:Clone() do
		newMap.Name = Player.Name
		local name = Instance.new("StringValue", newMap.params)
		name.Value = bunker_name
		local offset = Instance.new("NumberValue", newMap.params)
		--offset.Value = Player:WaitForChild("designated_bunker_position")
	end
	local terrain = newMap.terrain:WaitForChild("cubes")
	for i, v in pairs(terrainmaterialsStore:Get(default_bunker_materials)) do
		print (i) print(v)
		local Part = Instance.new("Part") do
			Part.Name = v
			print(terrainpositionStore:Get(default_object_position)) -- errors
			Part.CFrame = terrainpositionStore:Get(default_object_position)[i]
			Part.Size = terrainsizeStore:Get(default_bunker_terrain_size)[i]
			Part.Parent = terrain
			Part.Anchored = true
			Part.Transparency = 1
			newMap.Parent = workspace.maps
		end
	end  --------------------------------------------------------------------------------------------- function end
	
		function model_to_save(new_name)	
		local terrain = workspace.maps[Player.Name]terrain:WaitForChild("cubes")
		bunkernameStore = new_name

for i = 1, #terrain do
		local child = terrain[i]
		
		if child:IsA("Part") then
			terrainpositionStore[i] = child.Position
			terrainrotationStore[i] = child.Rotation
			terrainsizeStore[i] = child.Size
			terrainmaterialsStore[i] = child.Name
		end
	end

end
end
ReplicatedStorage:WaitForChild("load_map").OnServerEvent:Connect(function(player, bunker_name)
	save_to_model(player, bunker_name)
end)
	
	
end)





Mind marking which line errors?

Ok I am going to edit the post now.

Ok it turns out this is partially I used the object store instead of the terrain store. I really need to learn to spot these dumb mistakes.

I just realized I need to further serialize the CFrame values.

In ServerScriptService inside save_script on line 98 you should get CFrame but you got nil. Inside function :FillBlock() you have to insert some parameters which you can find which ones inside this API reference article.

1 Like

Nope nope all solved. It took a while but I serialized the CFrames and it worked. Also my load_map function is working fine right now, which is where I use :FillBlock().