I'm trying to create a messenger GUI that uses DataStores to get data across servers. I'm currently working on actually creating the DataStore, as well as giving it a bit more functionality, like giving it a key index and a way to monitor what happens to it.
datastore = game:GetService("DataStoreService"):GetDataStore("MessengerData")
Info log:
infolog = datastore:GetAsync("INFO_LOG")
if infolog == nil then
infolog = {}
setmetatable(infolog, {
__newindex = function(self, key, value)
rawset(self, key, value)
datastore:SetAsync("INFO_LOG", self)
end
})
datastore:SetAsync("INFO_LOG", infolog)
end
Key index:
index = datastore:GetAsync("KEY_INDEX")
if index == nil then
print("Initializing MessengerData KEY_INDEX for the first time...")
index = {}
setmetatable(index, {
__newindex = function(self, key, value)
rawset(self, key, value)
datastore:SetAsync("KEY_INDEX", self)
end
})
datastore:SetAsync("KEY_INDEX", index)
print("Initialized.")
table.insert(infolog, "Initialized KEY_INDEX at "..os.time())
end
And then I could just iterate over INFO_LOG to see what's been going on, and KEY_INDEX to see who's in the DataStore. I put a metatable on the index and the info log because I figured that every time I appended to those tables, I'd save them to their DataStore keys. Since I've saved a table with a metatable to the DataStore, would this shortcut apply to other scripts like this?
datastore = game:GetService("DataStoreService"):GetDataStore("MessengerData")
index = datastore:GetAsync("KEY_INDEX")
index.Bob = true
--Assuming the metatable is still there, I wouldn't have to put the SetAsync line here, right?
