DataStore2 table values printing as nil when set to a value

So I am using DataStore2 to transfer values to a different script, but this table value returns nil when it is set to something by default.

datastore script:

local Players = game:GetService("Players")
local ServerScriptService = game:GetService("ServerScriptService")
local ServerStorage = game.ServerStorage

local DataStore2 = require(ServerScriptService.DataStore2)

local userDataStoreName = "TestStore2"
DataStore2.Combine(userDataStoreName, "tables", "credits", "performancerank")

local userData = {

	Cars = {
		["Car1"] = {
			["Performance Index"] = math.random(100, 999);
			["Owned"] = false;
			["Name"] = "PlaceholderCar";
			["IsActive"] = true;
		};
	};
	
	GarageList = {};
	
	ActiveCar = "Car1"
}

local creditDefault = 5000

game.Players.PlayerAdded:Connect(function(player)
	local userStore = DataStore2("tables", player)
	local userTable = userStore:GetTable(userData)
	
	local creditsStore = DataStore2("credits", player)
	local levelStore = DataStore2("performancerank", player)
	
	
	local values = Instance.new("Folder", player)
    values.Name = "values"
	
	
	local credits = Instance.new("NumberValue", values)
    credits.Name = "Credits"
	
	credits.Value = creditsStore:Get(creditDefault)
	
	creditsStore:OnUpdate(function(newCredits)
		credits.Value = newCredits
	end)
	
	local level = Instance.new("NumberValue", values)
	level.Name = "PR"
	
	level.Value = levelStore:Get(1)
	
	
	userStore:Set(userTable)
	
	game.ReplicatedStorage.DataLoaded:Fire()
	print(userTable["Car1"]["Performance Index"])
end)

output:

ServerScriptService.DataStore2 - Script:60: attempt to index nil with ‘Performance Index’

I don’t understand why it happens but the parent table value is not nil so I don’t understand why this child one would be nil.

Your table would look like this template

local userData = {

	Cars = {
		["Car1"] = {
			["Performance Index"] = math.random(100, 999);
			["Owned"] = false;
			["Name"] = "PlaceholderCar";
			["IsActive"] = true;
		};
	};
	
	GarageList = {};
	
	ActiveCar = "Car1"
}

You meant

print(userTable["Cars"]["Car1"]["Performance Index"])
1 Like

You need to reference it properly like this:

print(userTable["Cars"]["Car1"]["Performance Index"])

Wow. I sometimes am really hard headed and don’t pay attention. Sorry for wasting your time with this.