DataStore2 Help

--// DataStore2 
local DataStore2 = require(script.DataStore2)
DataStore2.Combine("Xash","Death","Level")
--// Default Values
local DeathCount = 0
local XashCount = 10000
local LevelCount = 1






-- Metatable
local tab = {}

game.Players.PlayerAdded:Connect(function(plr)
	local XashData = DataStore2("Xash", plr)
	local DeathData = DataStore2("Death", plr)
	local LevelData = DataStore2("Level", plr)
	-- Metamethods
	setmetatable(tab,{
		__index = function(t,k)
			
			local folder = Instance.new("Folder")
			folder.Parent = plr
			folder.Name = "leaderstats"
			
			local Xash = Instance.new("IntValue")
			Xash.Parent = folder
			Xash.Name = "Xash"			
			
			local Level = Instance.new("IntValue")
			Level.Parent = folder
			Level.Name = "Level"
			
			local Death = Instance.new("IntValue")
			Death.Parent = folder
			Death.Name = "Death"
			
			
			
			return k
		end
	})
	
	getmetatable(tab)
	print("Indexing metamethod "..tab.t)
	wait(.5)
	

	local function saveDeath(update)
		plr.leaderstats.Death.Value = DeathData:Get(update)
	end
	local function saveXash(update)
		plr.leaderstats.Xash.Value = XashData:Get(update)
		
	end
	local function saveLevel(update)
		plr.leaderstats.Level.Value = LevelData:Get(update)
	end
	saveXash(XashCount)
	saveLevel(LevelCount)
	saveDeath(DeathCount)
end)

What am i doing wrong, i get an error saying

[ServerScriptService.Script.DataStore2:368: attempt to index local ‘tableResult’ (a number value)

Have a look in the Output, it should say what line it is. Then check to see if there is a red line under any of the words on X line.

Screenshot

Okay so, you click the red text and it should direct you.

So, when you’re combining you should be using a master key. Right now it looks like you’re missing it so you should do DataStore2:Combine("DATA", "Xash", "Death","Level") instead of DataStore2.Combine("Xash","Death","Level"). Be sure to reference Datastore2’s documentation in the future.

i get an error when i do that, plus in the site it says to do

DataStore2.Combine(masterKey, ...keysToCombine)

What error do you get? I replicated your code using the latest version of DataStore2 and it works fine for me. And yes, that’s what the site says and that’s what I’ve given you. In the sample I gave you DATA is the masterKey and everything else are the keysToCombine.

When i do DataStore2:Combine() then i get

[23:13:29.656 - ServerScriptService.Script.DataStore2:483: DataStore2() API call expected {string dataStoreName, Instance player}, got {table, Instance}]

Using the master key like this is undefined:

You should not be using the master key as a normal key, it should only ever be referenced in DataStore2.Combine.

1 Like