Attempt to call a nil value(at end of a table)

I’m making a saving system, and I keep getting this error

  22:42:46.487 - ServerScriptService.CurrencySystemsNewer:18: attempt to call a nil value

Here is the part of the script that causes the error:

local achievements = dataStore2:GetTable({
	
		["LobbyCoins"] = 0,
		["LobbyGems"] = 0,
		["LobbyCompleted"] = false,
							
		["GrasslandCoins"] = 0,
		["GrasslandGems"] = 0,
		["GrasslandCompleted"] = false,
		["GrasslandDeaths"]  = 0,
							
		["DesertCoins"] = 0,
		["DesertGems"] = 0,
		["DesertCompleted"] = false,
		["DesertDeaths"]  = 0,
							
		["CaveCoins"] = 0,
		["CaveGems"] = 0,
		["CaveCompleted"] = false,
		["CaveDeaths"] = 0,   -- here
		
	})

If I were to comment our the ["CaveDeaths"] = 0 part then it would error on the line above it. This is a script inside ServerScriptService. I can imagine this is an easy fix. Or does it have to relate to the datastore I’m using, DataStore2?

Have a good day!

This is probably wrong but I believe you add commas at the end if there is something you’ll add below, try getting rid of the comma at the end of it, it would also explain why you would still get the error getting rid of the [“CaveDeaths”] = 0, part

You should get rid of the comma at the end.

It doesn’t make a difference if there is an extra comma at the end as it gets ignored

Are you calling it on the DataStore2 table itself or on an instance of it? You should be doing the latter. Also you should be using combined data stores and saving a huge table defeats the purpose of combined data stores

I do use the combine function, but I’m not 100% sure what it does exactly. Correct me if I’m wrong, but does it combine all your data into one big table? Here is what I currently have:

local serverScriptService = game:GetService("ServerScriptService")
local serverStorage = game:GetService("ServerStorage")
local dataStore2 = require(serverScriptService.DataStore2)
local playerService = game:GetService("Players")

dataStore2.Combine("UserData","Coins","Gems","XP","Level","Trails","Achievements","Times")	

playerService.PlayerAdded:Connect(function(player)
	
	local coinData = dataStore2("Coins", player)
	local gemData = dataStore2("Gems", player)
	local levelData = dataStore2("Level", player)
	local xpData = dataStore2("XP", player)
	
	local achievements = dataStore2:GetTable({
-- here is just long lists of achievements
	})
	
	local times = dataStore2:GetTable({

-- another long list of times
	});
	
	local trails  = dataStore2:GetTable({
		-- long list of trails
	});

-- rest is just modifying and saving data OnUpdate

so instead do I just get rid of the dataStore2:GetTable() part?

Also not sure what you mean by:

I looked up instance on the developer hub and It didn’t really make sense to me.
I’m not a super advanced scripter so could you just reword this.

Have a good day!

The error you have means that the GetTable function is nil. This is the error one gets when one does

nil()

This means, in other words, that GetTable is not a function of dataStore2.

In OOP (Object-Oriented Programming) terms, the module DataStore2 is a class. You might have heard that term in the Roblox API Reference. For example, Part is a class. A class is a type. An instance is an individual.

If you’re calling GetTable() on the DataStore2 module itself (which, again, is a class), then it makes sense why it would not exist. GetTable() is only meant to be called on the DataStore2 instances, which can be created like this:

local statsStore = dataStore2("Statistics", player) -- All of the things in the table in your code could be stored as separate datastores and doesn't need to be stored as a single one

statsStore is an instance of dataStore2 and GetTable() can be called on it.

1 Like