DataStore2 Script Feedback Support

Hey, I hope this isn’t a hassle it’s a really short script that works based on my testing I was just wondering if someone can look over it?

It uses the DataStore2 module made by “Kampfkarren” (the popular one.) I just need a person(s) to make sure it’s okay sine this is my first time saving dictionaries (I think that’s the name) instead of each value in the combine.

The Script
local Players = game:GetService("Players")
local ServerScriptService = game:GetService("ServerScriptService")

local ExternalModules = ServerScriptService:WaitForChild("External Modules")
local DataStore2 = require(ExternalModules:WaitForChild("DataStore2"))

local Swords = {
	["Classic"] = 1,
	["Lightning"] = 0,
	["Red Lightning"] = 0
}

local Data = {
	["Deaths"] = 0,
	["Wins"] = 0,	
	["Top"] = 0,	
	["Playtime"] = 0,
	["Level"] = 0,
	["Experience"] = 0,
}

local function GetSwords(Player)
	local Swords = DataStore2("Swords", Player):Get({
		["Classic"] = 1,
	})
	
	return Swords
end

local function GetSettings(Player)
	return DataStore2("Data", Player):Get({
		["Deaths"] = 0,
		["Wins"] = 0,	
		["Top"] = 0,	
		["Playtime"] = 0,
		["Level"] = 0,
		["Experience"] = 0,
	})
end

local function SwordIncrement(ChosenValue, Player)
	local FunctionSwords = DataStore2("Swords", Player):Get({
		["Classic"] = 1,
	})

	if (not FunctionSwords[ChosenValue]) then
		FunctionSwords[ChosenValue] = 1
	else
		FunctionSwords[ChosenValue] += 1
	end

	DataStore2("Swords", Player):Set(FunctionSwords)
end

local function DataChange(ChosenValue, Player)
	local FunctionData = DataStore2("Data", Player):Get({
		["Deaths"] = 0,
		["Wins"] = 0,	
		["Top"] = 0,	
		["Playtime"] = 0,
		["Level"] = 0,
		["Experience"] = 0,
	}) 

	if (not FunctionData[ChosenValue]) then
		FunctionData[ChosenValue] = 1
	else
		FunctionData[ChosenValue] += 1
	end

	DataStore2("Data", Player):Set(FunctionData)
end

DataStore2.Combine("TestData2",
	"Time",
	"Kills",
	"Swords",
	"Data"
)

Players.PlayerAdded:Connect(function(Player)
	local TimeData = DataStore2("Time", Player)
	local KillsData = DataStore2("Kills", Player)
	local SwordsData = DataStore2("Swords", Player)
	local HiddenData = DataStore2("Data", Player)

	local Leaderstats = Instance.new("Folder")
	Leaderstats.Name = "leaderstats"

	local TimeValue = Instance.new("NumberValue")
	TimeValue.Name = "Time"
	TimeValue.Value = TimeData:Get(0)
	TimeValue.Parent = Leaderstats

	TimeData:OnUpdate(function(UpdatedValue)
		TimeValue.Value = UpdatedValue
	end)

	local KillsValue = Instance.new("NumberValue")
	KillsValue.Name = "Kills"
	KillsValue.Value = KillsData:Get(0)
	KillsValue.Parent = Leaderstats

	KillsData:OnUpdate(function(UpdatedValue)
		KillsValue.Value = UpdatedValue
	end)

	for i, v in pairs(GetSwords(Player)) do
		print(Player.Name.."'s Data ".. "["..i.."]".." = ".. v)
	end
	print("-")
	for i, v in pairs(GetSettings(Player)) do
		print(Player.Name.."'s Data ".. "["..i.."]".." = ".. v)
	end
	
	Leaderstats.Parent = Player
end)

Seems to look fine to me, however I’m not sure why you’re using a data table to store things such as, the player’s level, experience, wins and more. You should just store it in a single combine function as such:

Datastore2.Combine("MasteryKey", "Level", "Wins", "Top", "Playtime", ...)

And it’s also beneficial because let’s say you only want to get the player’s wins. To get the wins, you’d need to use:

Datastore2("Data", player):Get(defaultValue: any).Wins

Instead do the method I told you above, and store each data ‘object’ in the combine function so you can just do:

Datastore2("Wins", player):Get(0)

That’s a lot shorter right? And a lot cleaner to read, and more performant because you’re not requiring all the data.

And also, there’s a problem with using your method. For example, if we want to update the player’s wins let’s say… In the player’s attributes, you obviously need the :OnUpdate(callback) function from datastore2 to check when data has been updated.

So with your method, it would look something like this:

Datastore("Data", player):OnUpdate(function(newValue: any)
    player:SetAttribute("Wins", newValue.Wins)
end)

This is really just impractical. And also, it’s quite hard to update data, you even had to make your own function just to update whatever you need to update which is quite pointless when you could just be doing:

Datastore2("Wins", player):Increment(1, 0) -- 0 is here because it's the defaultValue.

But other than the data thing, your code is fine and doesn’t seem to look like it has any problems. If you have anymore questions, feel free to ask away.

1 Like

You’re always there to help me! You have really saved me on a lot of my development. Thank you I’m going to take into consideration and send back the edited script if you want to re-view.

Edit: Do you have discord?

Yes I do, Nonopter#5706. I think adding me would be better instead of allocating new threads each time.

1 Like