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

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

it saves but i am kinda worried

1 Like

Thanks for your feedback, could you try with deleting key2? It could be erroring because there is 2 keys. I’m going to work on it and see what could be causing the error.

still does not work when i delete key2 in the script

1 Like

i also get this error sometimes:
image

1 Like

Okay, thanks again. I’m going to look at the module and find whats wrong about it. It could be because of fast rejoin or data stores have issues. Either way I’m going to look at the module.

Thanks a lot, I knew more key support will cause errors. Working on a fix now.

Is there any extra data store scripts in your game? That could be causing modules data save to drop on the queue.

no i have none at the current time also my api is enabled

1 Like

Issues should have been fixed now. Can you try again?

did update the module?
charrrr

still super laggy when i re download it again

I updated the module. What did you meant by laggy? Does module lag your game?