Feedback on my DataStore Module

I’m experimenting with DataStore and want to figure out which is the best way to load and save data. Can I get some feedback on this Module?

local module = {}

local PlayerData = game:GetService("DataStoreService"):GetDataStore("Player_Data8")

module.LoadData = function(player)
	local Key = player.UserId
	local success, err = pcall(function()
		local Data = PlayerData:GetAsync(Key)
		local rPlayer = game:GetService("ReplicatedStorage").Players:WaitForChild(player.Name)
		if Data then
			player.leaderstats.Cash.Value = Data[1]
			player.leaderstats.Sugar.Value = Data[2]
			player.leaderstats["Cotton Candy"].Value = Data[3]
			player.Inventory.Value = Data[4]
			player.leaderstats.Rebirths.Value = Data[5]
			rPlayer.Cooldown.Value = Data[6]
			rPlayer.BackpackEquipped.Value = Data[7]
			rPlayer.ToolEquipped.Value = Data[8]
			print("Data loaded for " .. player.Name .. ".")
		else
			player.leaderstats.Cash.Value = 0
			player.leaderstats.Sugar.Value = 0	
			player.leaderstats["Cotton Candy"].Value = 0
			player.Inventory.Value = 10
			player.leaderstats.Rebirths.Value = 0
			rPlayer.Cooldown.Value = 10
			rPlayer.BackpackEquipped.Value = "Beginner Backpack"
			rPlayer.ToolEquipped.Value = "Scissors"
			local ValuesToSave = {
				player.leaderstats.Cash.Value,
				player.leaderstats.Sugar.Value,
				player.leaderstats["Cotton Candy"].Value,
				player.Inventory.Value,
				player.leaderstats.Rebirths.Value,
				rPlayer.Cooldown.Value,
				rPlayer.BackpackEquipped.Value,
				rPlayer.ToolEquipped.Value
			}
			PlayerData:SetAsync(Key, ValuesToSave)
			print(player.Name .. " is a new player. Data saved.")
		end
	end)
	if err then 
		warn("Data for player could not be loaded. " .. player.Name .. " will be kicked and notified.")
		player:Kick("DataStore error: Data could not be fetched. Please rejoin.")
	end
end

module.SaveData = function(player)
	local Key = player.UserId
	local rPlayer = game:GetService("ReplicatedStorage"):WaitForChild(player.Name)
	local success, err = pcall(function()
		local ValuesToSave = {
			player.leaderstats.Cash.Value,
			player.leaderstats.Sugar.Value,
			player.leaderstats["Cotton Candy"].Value,
			player.Inventory.Value,
			player.leaderstats.Rebirths.Value,
			rPlayer.Cooldown.Value
		}
		PlayerData:SetAsync(Key, ValuesToSave)
		print(player.Name .. "'s data has been saved.")
	end)
end

return module

Thanks :smiley:

2 Likes

It seems fine for a basic datastore system. I would recommend instead of hard-coding the values to save inside of the function, you store them in a table elsewhere and use a loop to create the save table. It will clean up the code a bit, especially if you end up saving hundreds of values.

2 Likes

I think your code would look better if you don’t do

module.func = function()

end

Rather, you should do its syntax sugar version

function module.func()

end
1 Like

Yea, the game does have a lot of data that needs saving. I will do that, thanks!

Thanks, I will do that!

Do be aware that this is pretty subjective. I personally use the first method at the moment. It’s really up to what the scripter prefers.

1 Like