Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- -- Services
- local dataStoreService = game:GetService("DataStoreService")
- -- Formatstrings
- local keyValue = "%s : %s"
- local requestError = "Unable to %s data for DataStore: %s"
- -- Private functions
- local function throwError(message)
- warn("From DSInterface: " .. message)
- end
- -- Tables
- local dataStoreInterface = {}
- dataStoreInterface.__index = dataStoreInterface
- -- Public object functions
- function dataStoreInterface:UnsafelyRequest(requestType, key, value)
- local dataStore = self.DataStore
- if requestType == "Set" then
- print("Setting, with ")
- print(key, value)
- dataStore:SetAsync(key, value)
- else
- return dataStore:GetAsync(key)
- end
- end
- function dataStoreInterface:SafelyRequest(requestType, key, value)
- local dataStore = self.DataStore
- local success, returnedValue = pcall(self.UnsafelyRequest, self, requestType, key, value)
- if not success then
- -- The get/set errored
- throwError(requestError:format(requestType, dataStore.Name))
- throwError(returnedValue)
- throwError(keyValue:format(key, value or ""))
- return false
- end
- -- In the case of Set, returnedValue will be nil at this point
- -- In the case of Get, returnedValue will be, well, the returned value.
- return returnedValue
- -- If there is a returnedValue, return that. If there isn't, return "success"
- end
- function dataStoreInterface:Set(...)
- return self:SafelyRequest("Set", ...)
- end
- function dataStoreInterface:Get(...)
- return self:SafelyRequest("Get", ...)
- end
- return {
- new = function(dataStore)
- return setmetatable(
- {
- DataStore = dataStoreService:GetDataStore(dataStore)
- },
- dataStoreInterface
- )
- end,
- }
Advertisement
Add Comment
Please, Sign In to add comment