LuckyDataStore - An Easy to Use Saving Module with Session Locking and Auto-Saving

Okay! I already added it to the module, going to publish it in a bit.

3 Likes

DataStoreModule v1.1 is here!

  • Optimized/cleaned the module.
  • Added a new value “SaveInStudio” which enables or disables studio saving.
  • Now GetDefault(value_variable, default_value) function could be used without giving it a default_value and script will define variables default value.
3 Likes

I’d recommend marking it as solution since there will be a lot of comments lol

3 Likes

Why should we use this over ProfileService or Datastore2, which is easier to use, more advanced, less data loss, etc. Auto-saving systems are always a problem. You should NEVER Use them, as they are prone to data loss, and it’s just lazy.

2 Likes

This is a “basic” module as in the name of the post, this is just done for beginners as using data stores are not that easy for them. They may exceed the limits of data store or can’t save data etc. This is made to help those people and you are able to disable auto-save if you don’t want it, also I’m not forcing anyone to use this so if you don’t want to use it then don’t.

5 Likes

Yes, but this module has a lot of edge cases and doesn’t do much to protect against data loss except for some few basic features which are incredibly easy to implement. As far as easy ness to use goes, DataStore2 is far easier to use for beginners and is much better than this as it at least protects against some sort of data loss, this doesn’t do to protect against against such data loss and is overall completely useless.

You are essentially trying to trap beginners over using this module which is bad and useless, when they can simply use a much better one.


This module has a lot of flaws from looking at the source code, doesn’t even utilise BindToClose properly and saving calls will almost be dropped when server shuts down. I highly discourage anyone from using this module.

7 Likes

I know that it is still not that much good and I’m still working on it. It is still “basic” but while I work on it I will make more protection about data loss and hopefully backups to revert when something bad occurs.

Why I would want to “trap” players? I said I’m still developing it and I know it is not the best.

4 Likes

If it’s basic, why make it as a community resource in the first place? Anyone can implement a very basic data store easily just like this, and there are a lot of resources over data stores.

4 Likes

[quote]This module has a lot of flaws from looking at the source code, doesn’t even utilise BindToClose properly and saving calls will almost be dropped when server shuts down. I highly discourage anyone from using this module.

Instead of saying this, why don’t you help me improve it? Your just trying to make the module look bad.

I said on the post that if there is any issue or suggestions, let me know.

4 Likes

I changed it because you said that it should not be on community resources. Where should it belong?

1 Like

No no, because you said that it could be bad using this module, I changed it so people won’t use it until I add more onto it.

And instead of saying the source code is bad or don’t use the module, tell me where I am wrong.

1 Like

This module isn’t as good as other modules for example DataStore2 or ProfileService, yes I agree with that. But it just shows that it’s “Basic” meaning it probably has bugs, glitches and more. Since beginners to DataStore cannot just yet learn about DataStore2 or ProfileService since they are a little bit more difficult to understand, I’ll say this module would be really good for them!

4 Likes

Thanks for your support, this was what I was trying to tell him but he says that source code is bad.

2 Likes

Also I don’t think it should be on the #help-and-feedback:code-review since you’re not reviewing this code to anybody, and you want beginners to learn from it. You’re not making a tutorial on how to do it, you’re just giving them the resource. Then why you put this in the #help-and-feedback:code-review?

2 Likes

I really don’t know where to put it, where should it belong?

2 Likes

Since this is a resource just put it in #resources:community-resources! Just be confident when you’re doing something!

3 Likes

Okay, thanks again for your support!

2 Likes

v1.2 has gone even better with

  • Multiple keys
  • And EnableBindToClose!

Well done on the update!

1 Like

Thanks a lot! What should I add more onto this on v1.3?

I have this script which works but the stores get overloaded. Im on v1.2 can i get some help?

local DataStoreModule = require(6989257174)

local key1 = DataStoreModule:CreateKey("TestKey")
local key2 = DataStoreModule:CreateKey("AnotherTestKey")

game.Players.PlayerAdded:Connect(function(player)

	local folder = Instance.new("Folder",player)
	folder.Name = "leaderstats"

	local money = Instance.new("IntValue",folder)
	money.Name = "cash"
	DataStoreModule:GetDefault(money,key1,100)

	DataStoreModule:GetData(player,key1,money)
end)

How can I fix it?

Module Script:

local module = {
	["AutoSaveEnabled"] = true,
	["AutoSaveCooldown"] = 180,
	["DebugMessages"] = false,
	["SaveInStudio"] = true,
	["EnableBindToClose"] = true,
}

local DataStoreTable = {}
local DefaultValues = {}

function module:CreateKey(key)
	DefaultValues[key] = {}
	return key
end

function module:GetDefault(value,key,default)
	if default then
		DefaultValues[key][value.Name] = default
		value.Value = default
	else
		DefaultValues[key][value.Name] = value.Value
	end
end

function module:GetData(player,key,...)
	if DataStoreTable[player.UserId] ~= nil then
	else
		DataStoreTable[player.UserId] = {}
	end
	DataStoreTable[player.UserId][key] = {...}
	local playerData = game:GetService("DataStoreService"):GetDataStore(key)
	local data
	local success, err = pcall(function()
		data = playerData:GetAsync("Player_"..player.UserId)
	end)
	if success then
		if data then
			for _, value in pairs(DataStoreTable[player.UserId][key]) do
				if data[value.Name] ~= nil then
					value.Value = data[value.Name]
				else
					value.Value = DefaultValues[key][value.Name]
				end
			end
			if module.DebugMessages then warn("Found player data and loaded successfully.")end
		else
			for _, value in pairs(DataStoreTable[player.UserId][key]) do
				value.Value = DefaultValues[key][value.Name]
			end
			if module.DebugMessages then warn("Couldn't find data for player.")end
		end
	else
		for _, value in pairs(DataStoreTable[player.UserId][key]) do
			value.Value = DefaultValues[key][value.Name]
		end
		DataStoreTable[player.UserId][key] = "Error"
		warn("Data loading failed on key "..key..". Using default values, data will not be saved. Error: "..err)
	end
end

game.Players.PlayerRemoving:Connect(function(plr)
	if not module.SaveInStudio and game:GetService("RunService"):IsStudio() then if module.DebugMessages then warn("Data has not been saved because SaveInStudio is false.")end return end
	for stringkey, key in pairs(DataStoreTable[plr.UserId]) do
		if key == "Error" then
			warn(stringkey.." data has not been saved due to fail load.")
		else
			local data_table = {}
			for _, value in pairs(key) do
				data_table[value.Name] = value.Value
			end
			local newData = game:GetService("DataStoreService"):GetDataStore(stringkey)
			local success, err = pcall(function()
				newData:SetAsync("Player_"..plr.UserId,data_table)
			end)
			if success then
				if module.DebugMessages then warn("Saved player data successfully.")end
			else
				warn("Error: "..err)
			end
		end
	end
	DataStoreTable[plr.UserId] = nil
end)

spawn(function()
	while wait(module.AutoSaveCooldown) do
		if module.AutoSaveEnabled then
			if not module.SaveInStudio and game:GetService("RunService"):IsStudio() then if module.DebugMessages then warn("Couldn't make an auto-save because SaveInStudio is false.")end return end
			if module.DebugMessages then warn("Starting an auto-save...")end
			for _, player in pairs(game.Players:GetPlayers()) do
				for stringkey, key in pairs(DataStoreTable[player.UserId]) do
					if key == "Error" then
						warn("Auto-save failed on key "..stringkey.." due to fail load.")
					else
						local data_table = {}
						for _, value in pairs(key) do
							data_table[value.Name] = value.Value
						end
						local newData = game:GetService("DataStoreService"):GetDataStore(stringkey)
						local success, err = pcall(function()
							newData:SetAsync("Player_"..player.UserId,data_table)
						end)
						if not success then
							warn("Error: "..err)
						end
					end
				end
			end
			if module.DebugMessages then warn("Auto-save finished successfully.")end
		end
	end
end)

game:BindToClose(function()
	if module.EnableBindToClose then
		for _, player in pairs(game.Players:GetPlayers()) do
			for stringkey, key in pairs(DataStoreTable[player.UserId]) do
				if key == "Error" then
				else
					local data_table = {}
					for _, value in pairs(key) do
						data_table[value.Name] = value.Value
					end
					local newData = game:GetService("DataStoreService"):GetDataStore(stringkey)
					local success, err = pcall(function()
						newData:SetAsync("Player_"..player.UserId,data_table)
					end)
				end
			end
		end
	end
end)

return module
1 Like