DataStore2 Error when saving data "attempt to call a table value"

Hello, Ive been working on my datastore and i keep getting this " attempt to call a table value]"
when trying to edit Inv Data, but editing Stats Data works completely fine… Im not sure whats going wrong here

calling it like this works

local DataStore = require(game.ServerScriptService.MainServerScript.Modules.DataHandler) DataStore.EditData(plr,"Points",2,true,false)

But calling it like this

local DataStore = require(game.ServerScriptService.MainServerScript.Modules.DataHandler) DataStore.EditData(plr,"LastLogin","158592686",false,true)

doesnt work

Here is the complete code if you want to check it out yourself

local Main = {}

local g = game
local Players = g.Players
local LocalPlayer = Players.LocalPlayer


--DataStore Start
local DataStore = require(script.DataStore2)
local Key = "wjdiaw2242"
DataStore.Combine(Key, "Stats1","Stats2","Inv1","Inv2","Pets")
-----------------


--Main DataStore Fucntion
local function SetDataTable()
	local UserData = {
		Stats1 = {
			["Points"] = 0,
			["Tokens"] = 0,
		},
		Stats2 = {
			["Points"] = 0,
			["Tokens"] = 0,
		},
		Inv1 = {
			["HitSpeed"] = 1,
			["HitTick"] = 1,
			["HitFinishValue"] = 50,
			["Supplies"] = 2,
			["BackPack"] = 2,
			["LastLogin"] = os.time(),
			["DaysInARow"] = 0,
		},
		Inv2 = {
			["HitSpeed"] = 1,
			["HitTick"] = 1,
			["HitFinishValue"] = 50,
			["Supplies"] = 2,
			["BackPack"] = 2,
			["LastLogin"] = os.time(),
			["DaysInARow"] = 0,
		},
		--PlaceHolder
		Pets = {
		
		
		},
	}
	return UserData
end
------------------------


--Init
local function Init_1(arg_1)
	print("[SERVER] DataHandler Started")
	Players.PlayerAdded:Connect(function(player)
		local UserData = DataStore(Key,player):Get(SetDataTable())
		
		--Public Data
		
		local leaderstats = Instance.new("Folder")
            leaderstats.Name = "leaderstats"
			
			local Points = Instance.new("IntValue",leaderstats)
			Points.Name = "Points"
			
			local Tokens = Instance.new("IntValue",leaderstats)
			Tokens.Name = "Coins"


		--Hidden Data


		local PerFolder = Instance.new("Folder")
            PerFolder.Name = "PerFolder"
			
			local HitSpeed = Instance.new("IntValue",PerFolder)
			HitSpeed.Name = "HitSpeed"
			
			local HitTick = Instance.new("IntValue",PerFolder)
			HitTick.Name = "HitTick"
			
			local HitFinishValue = Instance.new("IntValue",PerFolder)
			HitFinishValue.Name = "HitFinishValue"
			
			local Supplies = Instance.new("IntValue",PerFolder)
			Supplies.Name = "Supplies"
			
			local BackPack = Instance.new("IntValue",PerFolder)
			BackPack.Name = "BackPack"
		
			local LastLogin = Instance.new("IntValue",PerFolder)
			LastLogin.Name = "LastLogin"
			
			local DaysInARow = Instance.new("IntValue",PerFolder)
			DaysInARow.Name = "DaysInARow"
		
		repeat wait() until player.Team ~= game.Teams.Choosing
		local what
		if player.Team == game.Teams.Infected then
			what = 1
		else
			what = 2
		end
		local StatsData = DataStore("Stats"..what,player)
		local InventoryData = DataStore("Inv"..what,player)
		local Pets = DataStore("Pets",player)
		
		local function UpdateStats(UpVal)
			Points.Value = StatsData:Get(UpVal).Points
			Tokens.Value = StatsData:Get(UpVal).Tokens
		end
		local function UpdateInv(UpVal)
			HitSpeed.Value = InventoryData:Get(UpVal).HitSpeed
			HitTick.Value = InventoryData:Get(UpVal).HitTick
			HitFinishValue.Value = InventoryData:Get(UpVal).HitFinishValue
			Supplies.Value = InventoryData:Get(UpVal).Supplies
			BackPack.Value = InventoryData:Get(UpVal).BackPack
			LastLogin.Value = InventoryData:Get(UpVal).LastLogin
			DaysInARow.Value = InventoryData:Get(UpVal).DaysInARow
		end
		UpdateStats(UserData["Stats"..what])
		UpdateInv(UserData["Inv"..what])
		
		
		StatsData:OnUpdate(UpdateStats)
		InventoryData:OnUpdate(UserData)
		
		leaderstats.Parent = player
		PerFolder.Parent = player
		
	end)
end
------


--EditData
local function EditData(plr,data,value,stats,set)
	repeat wait() until plr.Team ~= game.Teams.Choosing
	local what
	if plr.Team == game.Teams.Infected then
		what = 1
	else
		what = 2
	end
	local UserData = DataStore(Key,plr):Get(SetDataTable())
	local StatsData = DataStore("Stats"..what,plr)
	local InventoryData = DataStore("Inv"..what,plr)
	local Pets = DataStore("Pets",plr)
	if stats then
		if set then
			UserData["Stats"..what][data] = value
		else
			UserData["Stats"..what][data] = UserData["Stats"..what][data] + value
		end
		StatsData:Set(UserData["Stats"..what])
	else
		if set then
			UserData["Inv"..what][data] = value
		else
			UserData["Inv"..what][data] = UserData["Inv"..what][data] + value
		end
		InventoryData:Set(UserData["Inv"..what])
	end
end
---------


Main.Init = Init_1
Main.EditData = EditData

return Main

Thank you for your time!

1 Like

Which specific line is not working?

The Function “EditData”

local function EditData(plr,data,value,stats,set)
	repeat wait() until plr.Team ~= game.Teams.Choosing
	local what
	if plr.Team == game.Teams.Infected then
		what = 1
	else
		what = 2
	end
	local UserData = DataStore(Key,plr):Get(SetDataTable())
	local StatsData = DataStore("Stats"..what,plr)
	local InventoryData = DataStore("Inv"..what,plr)
	local Pets = DataStore("Pets",plr)
	if stats then
		if set then
			UserData["Stats"..what][data] = value
		else
			UserData["Stats"..what][data] = UserData["Stats"..what][data] + value
		end
		StatsData:Set(UserData["Stats"..what])
	else
--error from here down \/
		if set then
			UserData["Inv"..what][data] = value
		else
			UserData["Inv"..what][data] = UserData["Inv"..what][data] + value
		end
		InventoryData:Set(UserData["Inv"..what])
	end
end

Just before that line,

print(InventoryData.Set)
1 Like

Prints a functionimage

Ok cool, and what is line 413?

line 413 of DataStore2 How to use DataStore2 - Data Store caching and data loss prevention


id doubt there would be something wrong with it

Then one of the things you passed to :OnUpdate was a table. Now send occurrences of :OnUpdate calls.

I just fixed it by doing this, I dont know why this works but who cares

local Main = {}

local g = game
local Players = g.Players
local LocalPlayer = Players.LocalPlayer


--DataStore Start
local DataStore = require(script.DataStore2)
local Key = "woewqawq1134"
DataStore.Combine(Key, "Stats1","Stats2","Pets")
-----------------


--Main DataStore Fucntion
local function SetDataTable()
	local UserData = {
		Stats1 = {
			["Points"] = 0,
			["Tokens"] = 0,
			["LastLogin"] = os.time(),
			["DaysInARow"] = 0,
			["HitSpeed"] = 1,
			["HitTick"] = 1,
			["HitFinishValue"] = 50,
			["Supplies"] = 2,
			["BackPack"] = 2,
		},
		Stats2 = {
			["Points"] = 0,
			["Tokens"] = 0,
			["LastLogin"] = os.time(),
			["DaysInARow"] = 0,
			["HitSpeed"] = 1,
			["HitTick"] = 1,
			["HitFinishValue"] = 50,
			["Supplies"] = 2,
			["BackPack"] = 2,
		},
		--PlaceHolder
		Pets = {
		
		
		},
	}
	return UserData
end
------------------------


--Init
local function Init_1(arg_1)
	print("[SERVER] DataHandler Started")
	Players.PlayerAdded:Connect(function(player)
		local UserData = DataStore(Key,player):Get(SetDataTable())
		
		--Public Data
		
		local leaderstats = Instance.new("Folder")
            leaderstats.Name = "leaderstats"
			
			local Points = Instance.new("IntValue",leaderstats)
			Points.Name = "Points"
			
			local Tokens = Instance.new("IntValue",leaderstats)
			Tokens.Name = "Coins"


		--Hidden Data


		local PerFolder = Instance.new("Folder")
            PerFolder.Name = "PerFolder"
			
			local HitSpeed = Instance.new("IntValue",PerFolder)
			HitSpeed.Name = "HitSpeed"
			
			local HitTick = Instance.new("IntValue",PerFolder)
			HitTick.Name = "HitTick"
			
			local HitFinishValue = Instance.new("IntValue",PerFolder)
			HitFinishValue.Name = "HitFinishValue"
			
			local Supplies = Instance.new("IntValue",PerFolder)
			Supplies.Name = "Supplies"
			
			local BackPack = Instance.new("IntValue",PerFolder)
			BackPack.Name = "BackPack"
		
			local LastLogin = Instance.new("IntValue",PerFolder)
			LastLogin.Name = "LastLogin"
			
			local DaysInARow = Instance.new("IntValue",PerFolder)
			DaysInARow.Name = "DaysInARow"
		
		repeat wait() until player.Team ~= game.Teams.Choosing
		local what
		if player.Team == game.Teams.Infected then
			what = 1
		else
			what = 2
		end
		local StatsData = DataStore("Stats"..what,player)
--		local InventoryData = DataStore("Inv"..what,player)
		local Pets = DataStore("Pets",player)
		
		local function UpdateStats(UpVal)
			Points.Value = StatsData:Get(UpVal).Points
			Tokens.Value = StatsData:Get(UpVal).Tokens
			HitSpeed.Value = StatsData:Get(UpVal).HitSpeed
			HitTick.Value = StatsData:Get(UpVal).HitTick
			HitFinishValue.Value = StatsData:Get(UpVal).HitFinishValue
			Supplies.Value = StatsData:Get(UpVal).Supplies
			BackPack.Value = StatsData:Get(UpVal).BackPack
			LastLogin.Value = StatsData:Get(UpVal).LastLogin
			DaysInARow.Value = StatsData:Get(UpVal).DaysInARow
		end
--		local function UpdateInv(UpVal)
--			HitSpeed.Value = InventoryData:Get(UpVal).HitSpeed
--			HitTick.Value = InventoryData:Get(UpVal).HitTick
--			HitFinishValue.Value = InventoryData:Get(UpVal).HitFinishValue
--			Supplies.Value = InventoryData:Get(UpVal).Supplies
--			BackPack.Value = InventoryData:Get(UpVal).BackPack
--			LastLogin.Value = InventoryData:Get(UpVal).LastLogin
--			DaysInARow.Value = InventoryData:Get(UpVal).DaysInARow
--		end
		UpdateStats(UserData["Stats"..what])
--		UpdateInv(UserData["Inv"..what])
		
		
		StatsData:OnUpdate(UpdateStats)
		
		leaderstats.Parent = player
		PerFolder.Parent = player
		
	end)
end
------


--EditData
local function EditData(plr,data,value,stats,set)
	repeat wait() until plr.Team ~= game.Teams.Choosing
	local what
	if plr.Team == game.Teams.Infected then
		what = 1
	else
		what = 2
	end
	local UserData = DataStore(Key,plr):Get(SetDataTable())
	local StatsData = DataStore("Stats"..what,plr)
	local Pets = DataStore("Pets",plr)
	if stats then
		if set then
			UserData["Stats"..what][data] = value
		else
			UserData["Stats"..what][data] = UserData["Stats"..what][data] + value
		end
		StatsData:Set(UserData["Stats"..what])
--	else
--		if set then
--			UserData["Inv"..what][data] = value
--		else
--			UserData["Inv"..what][data] = UserData["Inv"..what][data] + value
--		end
--		print(InventoryData.Set)
--		InventoryData:Set(UserData["Inv"..what])
	end
end
---------


Main.Init = Init_1
Main.EditData = EditData

return Main

2 Likes