[DataStore2] How to save an independent Table for the player

I’ve been experimenting with DataStore2 and I’ve been trying to come up with ways on how to create an independent table for the player and whether I’m on the right track or not.

On one of my previous posts, I was going to rely on the organization of folders within the Client and just leave it at that. But somebody pointed out it doesn’t seem very practical go along with that method. Additionally, the Client can exploit these values on their own if they were an exploiter.

So this time, I wanted to put everything in one table and so forth so it would be more secured in a sense:

ModuleScript

local PlayerDataMod = {

	currencyTable = {
		["Coins"] = 100,
		["Gems"] = 25,
	};
	
	allitemsTable = {
		"Default Emote",
		"Default Vehicle",
	};
}
return PlayerDataMod

Screen Shot 2020-06-22 at 8.39.53 PM
(If not secured, just another obstacle a typical exploiter would have to jump through.)

However, when I moved onto the data script, I got… stumped.

DataScript

local ServerScriptService = game:GetService("ServerScriptService")
local Players = game:GetService("Players")
local DataStore2 = require(1936396537)


Players.PlayerAdded:Connect(function(player)
	local PlayerDataModule = require(script.ModuleScript) --Confused/Stumped
	local PlayerDataStore = DataStore2("UserData",player)
	
	--?????
	
end)

The variable, PlayerDataModule, the table I’m grabbing, I’m very confused by this and don’t understand what I’m doing.

When I’m requiring a table from a ModuleScript, is the table a created instance on its own, or is it just editing whatever is in the ModuleScript?

Obviously this script doesn’t save data, but I’m really clueless on what to do here. I’m having a difficult time of trying to figure out what to do next.

First off you shouldn’t be requiring the DataStore2 module using this method because the free model in the toolbox is outdated. Instead you should be getting the latest module from the DataStore2 release page on GitHub: Releases · Kampfkarren/Roblox · GitHub

This is slightly incorrect. Exploiters can change anything on their client but with FilteringEnabled the changes are never replicated back to the server. This means you could have a folder on the client but only have the server update and read from it. This means if an exploiter does change any of the values it wont impact the server in any way because it will never see that the values have changed.

When you require a ModuleScript all the data inside it is cached so if you require the module from another script they will both be seeing the exact same values. In other words you can safely change the values in the ModuleScript and other scripts will see the values have changed.


I suggest you read through the DataStore2 documentation because this isn’t how you are supposed to use DataStore2: DataStore2

DataStore2 automatically keeps a cache of the players data internally so you shouldn’t be creating ModuleScripts of folders to store the data. Instead you should be creating a new data store in DataStore2 each time you have a new set of data and combine it to the master key. Documentation on what combine does and how DataStore2 works.

Here is an example of how you could use DataStore2 to create a simple leaderstats script:

local PlayersService = game:GetService("Players")

local DataStore2 = require(script.Parent.DataStore2)

DataStore2.Combine("MasterKey", "Coins") -- Combines the data to a master key
PlayersService.PlayerAdded:Connect(function(player)
	local coinsStore = DataStore2("Coins", player) -- Gets the coins data store
	
	local leaderstats = Instance.new("Folder")
	leaderstats.Name = "Leaderstats"
	
	local coins = Instance.new("NumberValue")
	coins.Name = "Coins"
	coins.Value = coinsStore:Get(0) -- Gets the coins value
	coins.Parent = leaderstats
	
	coinsStore:OnUpdate(function(newValue) -- Fires whenever the coins data store changes
		coins.Value = newValue
	end)
	
	leaderstats.Parent = player
end)

Then this could be used to change the players coins whenever the player clicks a part in workpace:

workspace.Part.ClickDetector.MouseClick:Connect(function(player)
	local coinsStore = DataStore2("Coins", player)
	coinsStore:Increment(1)  -- Adds 1 coin each time the part is clicked
end)

Here is another example of how DataStore2 could be used: Basic Example: Simulator - DataStore2

Bassically what you are trying to do DataStore2 already does it for you.

What’s funny is that in the module, it literally says to use the require by id to keep it up to date.

2 Likes