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.
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
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.