Error when trying to save a table on DataStore

  1. What do you want to achieve? Hello, i have a script that save table into a datastore for a leveling system, and i have an error. (The script was working perfectly until i added the test data in dataToSave, now when i launch the game it says : ServerScriptService.Main.Server.PlayerLevelHandler:37: attempt to index nil with ‘Level’)

  2. What is the issue? The issue is when i create another data in the table i get an error like : " attempt to index nil with ‘Level’ "

Here is the script :


game.Players.PlayerAdded:Connect(function(player)
	local EmployeeLevel = Instance.new("NumberValue")
	EmployeeLevel.Name = "EmployeeLevel"
	EmployeeLevel.Parent = player
	
	local EmployeeXP = Instance.new("NumberValue")
	EmployeeXP.Name = "CurrentXP"
	EmployeeXP.Parent = EmployeeLevel
	
	local MaxEmployeeXP = Instance.new("NumberValue")
	MaxEmployeeXP.Name = "MaximumXP"
	MaxEmployeeXP.Parent = EmployeeLevel
	
	local TestLevel = Instance.new("NumberValue")
	TestLevel.Name = "TestLevel"
	TestLevel.Parent = player

	local TestXP = Instance.new("NumberValue")
	TestXP.Name = "CurrentXP"
	TestXP.Parent = TestLevel

	local MaxTestXP = Instance.new("NumberValue")
	MaxTestXP.Name = "MaximumXP"
	MaxTestXP.Parent = TestLevel
	
	local data = nil
	local success, err = pcall(function()
		data = LevelsData:GetAsync(player.UserId)
	end)
	
	if success and data ~= nil then
		EmployeeLevel.Value = data.Employee.Level
		EmployeeXP.Value = data.Employee["Current XP"]
		MaxEmployeeXP.Value = data.Employee["Maximum XP"]
		TestLevel.Value = data.Test.Level
		TestXP.Value = data.Test["Current XP"]
		MaxTestXP.Value = data.Test["Maximum XP"]
	else
		EmployeeLevel.Value = 1
		EmployeeXP.Value = 0
		MaxEmployeeXP.Value = 20
		TestLevel.Value = 1
		TestXP.Value = 0
		MaxTestXP.Value = 20
	end

	
	EmployeeXP.Changed:Connect(function(value)
		if EmployeeXP.Value >= MaxEmployeeXP.Value then
			MaxEmployeeXP.Value = math.floor(MaxEmployeeXP.Value * 1.4)
			EmployeeLevel.Value = EmployeeLevel.Value + 1
			EmployeeXP.Value = 0
		end
	end)
end)

game.Players.PlayerRemoving:Connect(function(player)
	local dataToSave = {
		["Employee"] = {
			["Level"] = player.EmployeeLevel.Value,
			["Current XP"] = player.EmployeeLevel.CurrentXP.Value,
			["Maximum XP"] = player.EmployeeLevel.MaximumXP.Value
		},
		
		["Test"] = {
			["Level"] = player.TestLevel.Value,
			["Current XP"] = player.TestLevel.CurrentXP.Value,
			["Maximum XP"] = player.TestLevel.MaximumXP.Value
		}
	}
	
	local Success, err = pcall(function()
		LevelsData:SetAsync(player.UserId, dataToSave)
	end)
	
	if Success then
		print("data saved")
	end
end)
  1. What solutions have you tried so far? I tried to fix it myself and looked on forums.

If data already existed, it might not have Level saved. Do this instead:

		EmployeeLevel.Value = (data.Employee.Level) or 1

Basically the Employee table inside of data is nil. Could you try printing data?

Well, when i added this in the dataToSave table everything broke

["Test"] = {
   		["Level"] = player.TestLevel.Value,
   		["Current XP"] = player.TestLevel.CurrentXP.Value,
   		["Maximum XP"] = player.TestLevel.MaximumXP.Value
}

When the script was perfectly working i wanted to test if the script reset my data when i add other lines in the dataToSave Table but it broke

Could you add print(data) after you get the data and send us a screenshot of the result?

Wait something weird happened the test data appeared in the datastore and i don’t get the error anymore

To make sure that the error doesn’t happen anymore, you should start a local server with 1 player, because it wouldn’t have any data.

1 Like

I didn’t got any errors when trying this but im gonna add another data in dataToStore and you will see what happens

Ok so i added Testing Data that’s what the script look like now :

local LevelsData = game:GetService("DataStoreService"):GetDataStore("LevelsData")

game.Players.PlayerAdded:Connect(function(player)
	local EmployeeLevel = Instance.new("NumberValue")
	EmployeeLevel.Name = "EmployeeLevel"
	EmployeeLevel.Parent = player
	
	local EmployeeXP = Instance.new("NumberValue")
	EmployeeXP.Name = "CurrentXP"
	EmployeeXP.Parent = EmployeeLevel
	
	local MaxEmployeeXP = Instance.new("NumberValue")
	MaxEmployeeXP.Name = "MaximumXP"
	MaxEmployeeXP.Parent = EmployeeLevel
	
	local TestLevel = Instance.new("NumberValue")
	TestLevel.Name = "TestLevel"
	TestLevel.Parent = player

	local TestXP = Instance.new("NumberValue")
	TestXP.Name = "CurrentXP"
	TestXP.Parent = TestLevel

	local MaxTestXP = Instance.new("NumberValue")
	MaxTestXP.Name = "MaximumXP"
	MaxTestXP.Parent = TestLevel
	
	local TestingLevel = Instance.new("NumberValue")
	TestLevel.Name = "TestingLevel"
	TestLevel.Parent = player

	local TestingXP = Instance.new("NumberValue")
	TestXP.Name = "CurrentXP"
	TestXP.Parent = TestingLevel

	local MaxTestingXP = Instance.new("NumberValue")
	MaxTestingXP.Name = "MaximumXP"
	MaxTestingXP.Parent = TestingLevel
	
	local data = nil
	local success, err = pcall(function()
		data = LevelsData:GetAsync(player.UserId)
	end)
	
	if success and data ~= nil then
		EmployeeLevel.Value = data.Employee.Level
		EmployeeXP.Value = data.Employee["Current XP"]
		MaxEmployeeXP.Value = data.Employee["Maximum XP"]
		
		TestLevel.Value = data.Test.Level
		TestXP.Value = data.Test["Current XP"]
		MaxTestXP.Value = data.Test["Maximum XP"]
		
		TestingLevel.Value = data.Testing.Level
		TestingXP.Value = data.Testing["Current XP"]
		MaxTestingXP.Value = data.Testing["Maximum XP"]
	else
		EmployeeLevel.Value = 1
		EmployeeXP.Value = 0
		MaxEmployeeXP.Value = 20
		
		TestLevel.Value = 1
		TestXP.Value = 0
		MaxTestXP.Value = 20
		
		TestingLevel.Value = 1
		TestingXP.Value = 0
		MaxTestingXP.Value = 20
	end

	
	EmployeeXP.Changed:Connect(function(value)
		if EmployeeXP.Value >= MaxEmployeeXP.Value then
			MaxEmployeeXP.Value = math.floor(MaxEmployeeXP.Value * 1.4)
			EmployeeLevel.Value = EmployeeLevel.Value + 1
			EmployeeXP.Value = 0
		end
	end)
end)

game.Players.PlayerRemoving:Connect(function(player)
	local dataToSave = {
		["Employee"] = {
			["Level"] = player.EmployeeLevel.Value,
			["Current XP"] = player.EmployeeLevel.CurrentXP.Value,
			["Maximum XP"] = player.EmployeeLevel.MaximumXP.Value
		},
		
		["Test"] = {
			["Level"] = player.TestLevel.Value,
			["Current XP"] = player.TestLevel.CurrentXP.Value,
			["Maximum XP"] = player.TestLevel.MaximumXP.Value
		},
		
		["TestingData"] = {
			["Level"] = player.TestingLevel.Value,
			["Current XP"] = player.TestingLevel.CurrentXP.Value,
			["Maximum XP"] = player.TestingLevel.MaximumXP.Value
		}
	}
	
	local Success, err = pcall(function()
		LevelsData:SetAsync(player.UserId, dataToSave)
	end)
	
	if Success then
		print("data saved")
	end
end)

And that’s what i currently have on my datastore :
image

And it does the problem again look

When i add a new data to save inside the table it don’t find it

And this is what i have after leaving still the same
image

The problem is that when i create a new data to save in the table an join the script don’t find it

Is Employee, Test, or Testing on line 54?

Testing is on line 54 it’s the new data i created

The issue is that the player already has data, but not the new format. Which will cause an error only the first time they join without the newest data format.

Replace with this:

if data.Employee then
	EmployeeLevel.Value = data.Employee.Level
	EmployeeXP.Value = data.Employee["Current XP"]
	MaxEmployeeXP.Value = data.Employee["Maximum XP"]
end

if data.Test then
	TestLevel.Value = data.Test.Level
	TestXP.Value = data.Test["Current XP"]
	MaxTestXP.Value = data.Test["Maximum XP"]
end

if data.Testing then
	TestingLevel.Value = data.Testing.Level
	TestingXP.Value = data.Testing["Current XP"]
	MaxTestingXP.Value = data.Testing["Maximum XP"]
end

What do i need to replace with this ?

You should also make sure that you set the default data just in case.

		EmployeeLevel.Value = data.Employee.Level
		EmployeeXP.Value = data.Employee["Current XP"]
		MaxEmployeeXP.Value = data.Employee["Maximum XP"]
		
		TestLevel.Value = data.Test.Level
		TestXP.Value = data.Test["Current XP"]
		MaxTestXP.Value = data.Test["Maximum XP"]
		
		TestingLevel.Value = data.Testing.Level
		TestingXP.Value = data.Testing["Current XP"]
		MaxTestingXP.Value = data.Testing["Maximum XP"]
1 Like