· 7 years ago · Oct 17, 2018, 12:26 PM
1local databaseService = require(script.Parent.DatabaseService)
2local globalDatabase = databaseService:GetDatabase("Global")
3
4local function InsertDataToTable(tablename,folder)
5 for i,v in pairs(folder:GetChildren()) do
6 tablename[v.Name] = v.Value
7 end
8end
9
10function saveData(plr)
11 local tabl = {}
12 local tools = {}
13 local inventory = {}
14 local stats = {}
15
16
17 local data = game.ServerStorage._PlayerStore:FindFirstChild(plr.Name)
18 local statsFolder = data.StatsFolder
19 local toolsFolder = data.ToolsFolder
20 local inventoryFolder = data.InventoryFolder
21
22 InsertDataToTable(stats,statsFolder)
23 InsertDataToTable(tools,toolsFolder)
24 InsertDataToTable(inventory,inventoryFolder)
25
26 tabl["stats"] = stats
27 tabl["tools"] = tools
28 tabl["inventory"] = inventory
29
30 tabl = game:GetService("HttpService"):JSONEncode(tabl)
31 globalDatabase:PostAsync(plr.UserId, tabl,0)
32end
33function loadData(plr)
34 local tabl = {}
35 local tools = {}
36 local inventory = {}
37 local stats = {}
38
39 local mainFolder = Instance.new("Folder") -- load players folder into datastore folder
40 mainFolder.Parent = game.ServerStorage._PlayerStore
41 mainFolder.Name = plr.Name
42
43 local foldersTable = {script.StatsFolder:Clone(), script.ToolsFolder:Clone(),script.InventoryFolder:Clone()}
44 for i,v in pairs(foldersTable) do
45 v.Parent = mainFolder
46 end -- efficient way of loading all the folders into the mainfolder
47
48 local returnedData = globalDatabase:GetAsync(plr.UserId) -- get ata fro mthe sheets
49 if returnedData == nil then -- no keysso its a new player
50 saveData(plr) -- create a new save data
51 returnedData = globalDatabase:GetAsync(plr.UserId) -- continue with the returned data thanks
52 end
53 tabl = game:GetService("HttpService"):JSONDecode(returnedData)-- insert returned data into table
54
55 stats = tabl["stats"]
56 tools = tabl["tools"]
57 inventory = tabl["inventory"]
58
59 applyData(stats,foldersTable[1]) -- distributes the values fromthe sheets into the game
60 applyData(tools,foldersTable[2])--tools table distribution
61 applyData(inventory,foldersTable[3])--inventory table distribution
62 loadCharacter(plr,foldersTable[1]) -- load the character model
63end
64
65function loadCharacter(plr,statsfolder)
66
67 local char = script.StarterCharacter:Clone()
68 char.Parent = workspace.AlivePlayer
69 char.Name = math.random(1,1000)
70
71 local origPlayer = Instance.new("ObjectValue")
72 origPlayer.Value = plr
73 origPlayer.Name = "ActualPlayer"
74 origPlayer.Parent = char
75
76 char.Name = statsfolder.FirstName.Value .. " " .. statsfolder.LastName.Value
77
78 plr.Character = char
79-- game.ReplicatedStorage.Events.AssignCharacterToCamera:FireClient(plr,char.HumanoidRootPart)
80-- print"uwu"
81end
82function applyData(tablename,folder) -- basically creates missing values i could add support for cframe v3 color3 and brickcolor but im kinda lazy, maybe i will one day
83 for i,v in pairs(tablename) do
84
85 local instance = folder:FindFirstChild(tostring(i)) -- find instance
86 if instance then
87 --a designated value already exists.
88 instance.Value = v -- give the value the value from the data store
89 elseif instance == nil then
90 --we need to make the value because it does not exist
91 local newValue = workspace
92 if tonumber(v) == nil then
93 newValue = Instance.new("StringValue")
94 elseif tonumber(v) ~= nil then
95 --so we could convert it to a number, meaning the avlue isnt a string
96 newValue = Instance.new("IntValue")
97 end
98 newValue.Name = i
99 newValue.Value = v
100 newValue.Parent = folder
101 end
102 end
103end
104
105game.Players.PlayerAdded:Connect(function(plr)
106 loadData(plr)
107end)
108game.Players.PlayerRemoving:Connect(function(plr)
109 saveData(plr)
110end)