DataStore2 with Tables

integrating tables with the normal Datastore is important but what about Datastore2

  • First setting up the table

local DataStore2 = require(1936396537)

local defaultValue = 0

   --table
game.Players.PlayerAdded:Connect(function(plr)
local PlayerData = {["Money"]=DataStore2("Money", plr),
				["Power"]=DataStore2("Power", plr)}

end)


  • Next LeaderBoard if you want
  • Also update the LeaderBoard

game.Players.PlayerAdded:Connect(function(plr)
local PlayerData = {["Money"]=DataStore2("Money", plr),
					["Power"]=DataStore2("Power", plr)}

--leaderboard
local stats = Instance.new("Folder",plr);stats.Name = "leaderstats"
	for i,v in pairs(PlayerData)do
		local value = Instance.new("IntValue",stats);
		value.Name=i;
	end

    --Update leaderboard
for i,v in pairs(PlayerData)do
local function DataUpdate(DataValue)
	plr.leaderstats[i].Value = PlayerData[i]:Get(DataValue)
end

DataUpdate(defaultValue)

PlayerData[i]:OnUpdate(DataUpdate)
end

end)

  • Simple Click Block For testing

--I but this within the same script but all you have to do is  make sure you have 
--|local DataStore2 = require(1936396537)| and you should be good both ways :)
workspace.ClickPart.ClickDetector.MouseClick:Connect(function(plr)
	local PlayerData = {["Money"]=DataStore2("Money", plr),
					["Power"]=DataStore2("Power", plr)}
PlayerData["Money"]:Increment(50,defaultValue)
PlayerData["Power"]:Increment(5,defaultValue)

end)

simple helpful tutorial any question just ask me on devform @PhantumDev maybe able to help

6 Likes

You’d actually be better off storing the table in a single DataStore key, for single values like this. It’s a bit more work in terms of not having :Increment() and such available to you within the data table, but it’ll use less of the budget when saving/loading data

2 Likes

Also according to kampfkarren he doesn’t update the free model due to a Roblox bug, so getting latest version off the github is a better idea.

1 Like