How do I index saved arrays?

I’m loading arrays from DataStore2 that contain part properties. They then get put into a function that applies the properties to the parts. Problem is that scripts don’t seem to like to index saved arrays so I have to probably format it differently. I have noticed that the script crashes on line 83 and not line 80 where I have the datastore variable first defined so it is definitely a problem with indexing.

Script (I’ll label lines 80 and 83)

ServerStorage = game:GetService("ServerStorage")
Players = game:GetService("Players")

default_omega_coins, default_likes, default_wins = 0, 0, 0

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", "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)
	
	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 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(folder_name, target_location, CFrameoffset, pos,size,rot,mat)
	local newMap = ServerStorage.empty_map:Clone() do
		newMap.Name = folder_name
	end
	
	local terrain = newMap.terrain:WaitForChild("cubes") 
	for i, v in pairs(terrainmaterialsStore:Get(default_bunker_materials)) do --80
		local Part = Instance.new("Part") do
			Part.Name = v
			Part.CFrame = pos[i] --83
			Part.Size = size[i]
			Part.Parent = terrain
			
		end
	end
	
	newMap.Parent = target_location
end
	
	save_to_model("MrGuyROBLOX's Dungeon", game.Workspace.maps, 0, 0, 0, terrainpositionStore:Get(default_bunker_terrain_position), terrainsizeStore:Get(default_bunker_terrain_size), terrainrotationStore:Get(default_bunker_terrain_rotation),terrainmaterialsStore:Get(default_bunker_materials)) -- test function
	
end)

What’s the error?

30char

1 Like

The error is: 11:06:22.391 - ServerScriptService.save_script:83: attempt to index number with number

Your function save_to_model expects 7 arguments, but in your function call you’re passing in 9.

What you’re passing in as ‘pos’ is actually ‘0’ instead of which I assume you want terrainpositionStore:Get(default_bunker_terrain_position). Hence why your error states ‘attempt to index a number with number’ As you’re trying to index 0.

1 Like

Oh I don’t have any errors now. Cframeoffset is unused at the moment as I have no idea how I should implement it.

If my post solved your problem, please mark it as the solution. Thank you.

If you’re specifying a CFrameOffset, typically this would be multiplied to an existing CFrame value.

Wrap each of the ‘0’ arguments within a CFrame.new(0, 0, 0) and pass that in as its own argument, rather than 3 individual arguments. That way, your function call will be passing in 7 arguments which your function is expecting.

1 Like