Attempt to index nil with Level

Strange that I’ve been getting this error. Here are my 2 functions.

local function SetData(player)
	
	local key = string.format("-Player_Key: %s", player.UserId)
	local attempts = 0
	
	if not player.Data.Loaded.Value then
		return false
	end
	
	local data = {
		Level = player.leaderstats.Level.Value,
	}
		
	repeat wait()
		
	local success, errormessage = pcall(function()
		PlayerDataStores:SetAsync(key, data)
	end)
			
	if success then
		print(string.format("Data successfully saved for %s", player.Name))
	else
		attempts += 1	
	end
	
	until success or attempts	 == 3
end


local function GetData(player)
	local key = string.format("-Player_Key: %s", player.UserId)
	local data
	local attempts = 0
	
	repeat wait()
		
		local success, errormessage = pcall(function()
			data = PlayerDataStores:GetAsync(key)
		end)
		
		if success then
			player.leaderstats.Level.Value = data.Level
			player.Data.Loaded.Value = true
		else 
			attempts += 1
		end
		
	until success or attempts >= 3
end

player.leaderstats.Level.Value = data.Level – Line where it errors


13:20:32.355 - ServerScriptService.Silent Data:75: attempt to index nil with 'Level'
1 Like

Try and change your code to this:

local function SetData(player)
	
	local key = string.format("-Player_Key: %s", player.UserId)
	local attempts = 0
	
	if not player.Data.Loaded.Value then
		return false
	end
	
	local data = {
		Level = player.leaderstats.Level.Value,
	}
		
	repeat wait()
		
	local success, errormessage = pcall(function()
		PlayerDataStores:SetAsync(key, data)
	end)
			
	if success then
		print(string.format("Data successfully saved for %s", player.Name))
	else
		attempts += 1	
	end
	
	until success or attempts	 == 3
end


local function GetData(player)
	local key = string.format("-Player_Key: %s", player.UserId)
	local data
	local attempts = 0
	
	repeat wait()
		
		local success, errormessage = pcall(function()
			data = PlayerDataStores:GetAsync(key)
		end)
		
		if success then
			player:WaitForChild("leaderstats").Level.Value = data.Level
			player.Data.Loaded.Value = true
		else 
			attempts += 1
		end
		
	until success or attempts >= 3
end

That’s not the problem, the level in data is nil.

1 Like

Try Using WaitForChild() with data.Level

1 Like

Well, the error the output is returning is it can’t index ‘Level’. You try to index a children of nil.

Data is a table. He has to reference the key ‘Level’ in the data table.

The problem is that data is nil, not data.Level.

1 Like

Try this:

local function SetData(player)
	
	local key = string.format("-Player_Key: %s", player.UserId)
	local attempts = 0
	
	if not player.Data.Loaded.Value then
		return false
	end
	
	local data = {
		Level = player.leaderstats.Level.Value,
	}
	
	repeat wait()
		
		local success, errormessage = pcall(function()
			PlayerDataStores:SetAsync(key, data)
		end)
		
		if success then
			print(string.format("Data successfully saved for %s", player.Name))
		else
			attempts += 1	
		end
		
	until success or attempts	 == 3
end


local function GetData(player)
	local key = string.format("-Player_Key: %s", player.UserId)
	local data
	local attempts = 0
	
	repeat wait()
		
		local success, errormessage = pcall(function()
			data = PlayerDataStores:GetAsync(key) or 0
		end)
		
		if success then
			player:WaitForChild("leaderstats").Level.Value = data.Level
			player.Data.Loaded.Value = true
		else 
			attempts += 1
		end
		
	until success or attempts >= 3
end

In the GetData() function, you are checking if the pcall was a success, not if the data exists. If it’s a player’s first time playing, the data of course does not exist. Change:

if success then

To:

if success and data then
1 Like

Yes, I was thinking of doing that.

Also,

data = PlayerDataStores:GetAsync(key) or 0

Remove the “or 0” there. Lower down in the code, you’re trying to do

data.Level

And if you set data to “0”, then data.Level is nil, causing an error. Instead, change

player:WaitForChild("leaderstats").Level.Value = data.Level

To

player:WaitForChild("leaderstats").Level.Value = data.Level or 0

Hopefully this helps!

I don’t think there is a reason for or 0 since I’m already checking if data exists. The level value will be 1 by default.

I was just making an example based on your code; you can change it however you like.