Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- 1st script
- -- Services.
- local ServerStorage = game:GetService("ServerStorage")
- local ReplicatedStorage = game:GetService("ReplicatedStorage")
- local DataStoreService = game:GetService("DataStoreService")
- -- Classes.
- local Classes = ServerStorage.Server.Classes
- local Response = require(Classes.Response)
- -- Data.
- local Data = ServerStorage.Server.Data
- local DataStoreData = require(Data.DataStoreData)
- -- Functions.
- local function GetRequestBudget(request_type)
- return DataStoreService:GetRequestBudgetForRequestType(DataStoreData.RequestTypeMap[request_type])
- end
- local function DataRequest(request_type, key, datastore, value) -- Returns a response object.
- local request_count = 0
- local success, data
- repeat
- while GetRequestBudget(request_type) <= 0 do
- wait()
- end
- success, data = pcall(datastore[request_type], datastore, key, value)
- if not success then
- warn(data)
- wait(1)
- else
- print(data)
- local response = Response.new(true, data)
- return response
- end
- until request_count == DataStoreData.MaxSuccessiveRetry or success
- -- Append to failed requests and construct a bad response.
- if not DataStoreData.FailedRequests[key] then
- DataStoreData.FailedRequests[key] = true
- end
- -- Construct bad response.
- local bad_response = Response.new(false, nil, string.sub(data, 1, 3), string.sub(data, 3, string.len(data)))
- return bad_response
- end
- -- Lib.
- local DataStoreLib = {}
- function DataStoreLib.GetData(key, datastore)
- return DataRequest("GetAsync", key, datastore)
- end
- function DataStoreLib.SetData(key, datastore, value) -- Prevent data from being set if there was an issue loading the data, and an empty schema was used.
- -- Set data if there is no record of any failed requests.
- if not DataStoreLib.IsRequestFailed(key) then
- return DataRequest("SetAsync", key, datastore, value)
- end
- -- Attempt to get more recent value (in case there was a problem getting data a previous time.)
- local updated_value
- local updated_fetch_response = DataRequest("GetAsync", key, datastore)
- -- Send another response if the fetch attempt failed.
- if not updated_fetch_response:IsSuccess() then
- return Response.new(false, nil, 400, "Unable to fetch updated data prior to saving.")
- end
- -- If the fetch attempt succeeded, attempt to set the value to whatever was recieved.
- updated_value = updated_fetch_response:GetPayload()
- local set_repsonse = DataRequest("SetAsync", key, datastore, updated_value)
- -- If the set attempt failed, return a bad response along with the updated value fetched previously.
- if not set_response:IsSucess() then
- return Response.new(false, updated_value, 400, "Unable to set data.")
- end
- end
- function DataStoreLib.IsRequestFailed(key)
- if key == nil then return end
- return DataStoreData.FailedRequests[key]
- end
- function DataStoreLib.ClearFailedRequests(key)
- if key == nil then return end
- DataStoreData.FailedRequests[key] = nil
- end
- return DataStoreLib
- 2nd script local DataStoreData = {}
- DataStoreData.IsInProduction = false
- DataStoreData.PlayerSchemaVersion = 0 -- Redundant right now.
- DataStoreData.MaxSuccessiveRetry = 3
- DataStoreData.RequestTypeMap = {
- OnUpdate = Enum.DataStoreRequestType.OnUpdate,
- GetAsync = Enum.DataStoreRequestType.GetAsync,
- UpdateAsync = Enum.DataStoreRequestType.UpdateAsync,
- SetAsync = Enum.DataStoreRequestType.SetIncrementAsync,
- GetSortedAsync = Enum.DataStoreRequestType.GetSortedAsync,
- IncrementAsync = Enum.DataStoreRequestType.SetIncrementAsync,
- SetIncrementAsync = Enum.DataStoreRequestType.SetIncrementAsync,
- SetIncrementSortedAsync = Enum.DataStoreRequestType.SetIncrementSortedAsync
- }
- DataStoreData.FailedRequests = {} -- key: has_failed, has_failed is a boolean which states whether or not any request to this key has failed, is set to false once resolved.
- return DataStoreData
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement