· 5 years ago · Jun 05, 2020, 01:12 PM
1---------------------------------------------------------------------------------------------------
2-- Written by: Ryan S. <GD6_Ryisng | glocation87@gmail.com> --
3-- Description: Data Class allows instantiation of a new DataStore with the provided identifier, --
4-- all objects of this class corresponds to the class' datastore. --
5---------------------------------------------------------------------------------------------------
6
7--Services
8local DataService = game:GetService("DataStoreService")
9
10--Dependencies
11local Templates = require(script.Templates)
12
13--Class
14local Data = {}
15
16-- meta data to be assigned to class
17local data_meta = {
18 -- when we call the table, we create the data store, and the global list.
19 -- as well as return the corresponding class methods
20 -- Reminder to add functionality for Global and ordered data stores
21 __call = function(class, store)
22 assert(type(store) ~= string, "Argument 1 must be of type <string>")
23 local global_list = {}
24 local methods = {}
25 local _store = DataService:GetDataStore(store)
26
27 -- Function to load in the player's data, given the key(player's user id)
28 function methods.load(key)
29 local data
30 -- wrap in a protected to call
31 local success, err = pcall(function()
32 -- retrieve data from store
33 data = _store:GetAsync(key)
34 end)
35 if err then
36 data = nil
37 end
38
39 -- if data is nil then return
40 -- a base template if one exists
41 -- or an empty table
42 if data == nil then
43 data = Templates[store] or {}
44 end
45
46 --store the data in our global list
47 global_list[key] = data
48 end
49
50 -- Function to return the player's data from the global list
51 function methods._return(userId, field)
52 if not field then
53 return global_list[userId]
54 end
55 return global_list[userId][field]
56 end
57
58 -- Function to save the player's corresponding data to the datastore
59 function methods.save(userId)
60 -- In the event that the player does not have any data, from the global list
61 -- if a template exists for the data class we'll save that instead
62 local data = global_list[userId] or Templates[store]
63
64 local success, err = pcall(function()
65 -- save to datastore
66 _store:SetAsync(userId, data)
67 end)
68
69 if success then
70 -- maybe add a print here later
71 elseif err then
72 -- error handling to be implemented
73 end
74 end
75
76 -- Function to create a new data entry field or edit a pre existing one
77 function methods.edit(userId, field, newValue)
78 global_list[userId][field] = newValue
79 end
80
81 -- Function to reset the specified player's data
82 function methods.reset(userId)
83 global_list[userId] = Templates[store] or {}
84
85 local success, err = pcall(function()
86 _store:SetAsync(userId, global_list[userId])
87 end)
88
89 if success then
90 -- maybe add a print here later
91 elseif err then
92 -- error handling to be implemented
93 end
94 end
95
96 return methods
97 end
98}
99
100--Assign metatable to class
101setmetatable(Data, data_meta)
102--Return class
103return Data