DataStore2 Help v2

local DataStore2 = require(script.DataStore2)
local xash = "xash"
local level = "level"
local death = "death"
DataStore2.Combine(xash,level,death)

local tab = {}
local XashValue = 100
game.Players.PlayerAdded:Connect(function(plr)
	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 Death = Instance.new("IntValue")
		Death.Parent = folder
		Death.Name = "Death"
		
		local Level = Instance.new("IntValue")
		Level.Parent = folder
		Level.Name = "Level"
		return k
		
	end	
	})
	print("Indexing Metamethods "..tab.METAMETHOD)
	local Level = plr.leaderstats.Level
	local Death = plr.leaderstats.Death
	local Xash = plr.leaderstats.Xash
	if Xash == nil then
		Xash = plr.leaderstats.Xash
	end
	
	local function levelSave(update)
		Level.Value = DataStore2(level,plr):Get(update)
	end
	local function DeathSave(update)
		Death.Value = DataStore2(death,plr):Get(update)
	end
	local function XashSaver(update)
		plr.leaderstats.Xash.Value = DataStore2(xash,plr):Get(update)
	end
	local XashDataStore = DataStore2(xash,plr)
	
	levelSave(DataStore2(level,plr):Get(1))
	DeathSave(DataStore2(death,plr):Get(1))
	XashSaver(XashDataStore:Get(100))
	
	DataStore2(level,plr):OnUpdate(levelSave())
	DataStore2(death,plr):OnUpdate(DeathSave())
	DataStore2(xash,plr):OnUpdate(XashSaver())
	
	
end)

THE SCRIPT WORKS BUT EVERYTIME I JOIN THE XASH VALUE IS ALWAYS SET TO 0, Please help

What you are doing here, is passing the results of these function calls to :OnUpdate.

:OnUpdate requires a function, so all you need to do is remove the parentheses that call the functions.

DataStore2(level,plr):OnUpdate(levelSave)
DataStore2(death,plr):OnUpdate(DeathSave)
DataStore2(xash,plr):OnUpdate(XashSaver)

Also why are you making 2 :Get calls here? One would suffice. By the way you can save tables with DataStore2, so hard coding for each value is redundant.

Finally, why are you using metatables like this? What is the purpose here?

1 Like