How to improve my datastore script

Hey guys, I’m working on a game and I was wondering how to make my script more efficient.

code:

local DataStoreService = game:GetService("DataStoreService")
local datadict = {
	["Dehydration"] = function (player) 
		local datastore = DataStoreService:GetDataStore("Dehydration")
		local success, err = pcall(function()
			datastore:SetAsync(player.UserId, true)
		end)
		if success then
			print("save successful")
		else
			print("error occured")
			warn(err)
		end
	end,
	["Milk"] = function (player) 
		local datastore = DataStoreService:GetDataStore("Milk")
		local success, err = pcall(function()
			datastore:SetAsync(player.UserId, true)
		end)
		if success then
			print("save successful")
		else
			print("error occured")
			warn(err)
		end
	end,
	["Spoiled Milk"] = function (player) 
		local datastore = DataStoreService:GetDataStore("Spoiled Milk")
		local success, err = pcall(function()
			datastore:SetAsync(player.UserId, true)
		end)
		if success then
			print("save successful")
		else
			print("error occured")
			warn(err)
		end
	end,
	["Cheese"] = function (player) 
		local datastore = DataStoreService:GetDataStore("Cheese")
	local istrue 
		local success, err = pcall(function()
			istrue = datastore:SetAsync(player.UserId, true)
		end)
		if success then
			print("save successful")
		else
			print("error occured")
			warn(err)
		end
	end,
}

Basically I just have each function that deals with saving a data in a dictionary and I can call it by using the key.

How would I make this process more efficient?

Well, saving separate data in separate key is usually not really ideal.

If you can handle your data on a dictionary that would be way better, for instance if you were to use ProfileService, it’s just a matter of changing a value inside a dictionary.

ProfileService isn’t hard to learn, if you want a good data handler, it’s a great module to use.

The problem is that with multiple datastore keys to handle data that could be otherwise saved in a dictionary, means that there could be inconsistency. If a key didn’t save properly that means that now you can’t confirm what is true and what isn’t. If you save it into a dictionary, that means that everything is right in the point in time that dictionary was saved.

Anyhow it’s hard to explain but it’s better to use tables for player data, so that their data is right or at least just slightly wrong consistenly. It’s also just easier to handle afterwards to handle data anyhow; it’s just overall better to the point that it’s hard to give a reason because it’s so obvious once you realize it

1 Like

Excuse me if im wrong, but aren’t keys a part of a dictionary?

Keys are just the “location” at which the values in a table are stored at. All tables have the concept of keys, it’s not limited to just a dictionary. The difference is that arrays can have only numerical sequential keys while dictionaries can have both string and number keys as well.

so the index of something in an array is basically like a numerical key?

Yes, a numerical key assuming they are from a-z, a being the first valid index of the array while z bring the last.

What I’m confused at is that the guy said that I would be better putting the data in a dictionary, but I thought I was putting it in a dictionary?