This Code Saves Data, but it doesn't. Can anyone help?
Asked by
4 years ago Edited 4 years ago
there are no errors and the game just wont save data
001 | local dataStore = game:GetService( "DataStoreService" ):GetDataStore( "MyMiningData" ) |
002 | local capacityEvent = game.ReplicatedStorage:WaitForChild( "UICapacityEvent" ) |
003 | local boughtRequest = game.ReplicatedStorage:WaitForChild( "BoughtRequest" ) |
007 | function onSpawn(character) |
008 | local player = game.Players:GetPlayerFromCharacter(character) |
009 | local pickaxe = player:WaitForChild( "Pickaxe" ).Value |
010 | local backpack = player:WaitForChild( "BackpackItem" ).Value |
011 | local newPickaxe = game.ReplicatedStorage:FindFirstChild(pickaxe):Clone() |
012 | newPickaxe.Parent = player.Backpack |
013 | local newBackpack = game.ReplicatedStorage:FindFirstChild(backpack):Clone() |
014 | player.Character.Humanoid:AddAccessory(newBackpack) |
015 | local capacity = newBackpack.Capacity.Value |
016 | capacityEvent:FireClient(player, capacity) |
018 | function onPlayerEntered(player) |
019 | local initKey = "user_" .. player.userId .. "_init" |
020 | local coinsKey = "user_" .. player.userId .. "_coins" |
021 | local minedKey = "user_" .. player.userId .. "_mined" |
022 | local invenValKey = "user_" .. player.userId .. "_invenVal" |
023 | local invenCountKey = "user_" .. player.userId .. "_invenCount" |
024 | local backpackKey = "user_" .. player.userId .. "_backpack" |
025 | local pickaxeKey = "user_" .. player.userId .. "_pickaxe" |
026 | local boughtKey = "user_" .. player.userId .. "_bought" |
027 | local rebirthKey = "user_" .. player.userId .. "_rebirth" |
028 | local rebirthCKey = "user_" .. player.userId .. "_rebrithC" |
029 | if (dataStore:GetAsync(initKey) = = nil or debugMode = = true ) then |
030 | dataStore:SetAsync(initKey, true ) |
031 | dataStore:SetAsync(coinsKey, 0 ) |
032 | dataStore:SetAsync(minedKey, 0 ) |
033 | dataStore:SetAsync(invenValKey, 0 ) |
034 | dataStore:SetAsync(invenCountKey, 0 ) |
035 | dataStore:SetAsync(backpackKey, "BackpackBasic" ) |
036 | dataStore:SetAsync(pickaxeKey, "Basic Pickaxe" ) |
037 | dataStore:SetAsync(boughtKey, { } ) |
038 | dataStore:SetAsync(rebirthKey, 0 ) |
039 | dataStore:SetAsync(rebirthCKey, 10000000 ) |
041 | local init = dataStore:GetAsync(initKey) |
042 | local mined = dataStore:GetAsync(minedKey) |
043 | local coins = dataStore:GetAsync(coinsKey) |
044 | local invenVal = dataStore:GetAsync(invenValKey) |
045 | local invenCount = dataStore:GetAsync(invenCountKey) |
046 | local backpack = dataStore:GetAsync(backpackKey) |
047 | local pickaxe = dataStore:GetAsync(pickaxeKey) |
048 | local bought = dataStore:GetAsync(boughtKey) |
049 | local rebirth = dataStore:GetAsync(rebirthKey) |
050 | local rebirthC = dataStore:GetAsync(rebirthCKey) |
051 | _G.boughtArray [ player.userId ] = bought |
052 | local leaderstats = Instance.new( "Folder" ) |
053 | leaderstats.Name = "leaderstats" |
054 | leaderstats.Parent = player |
055 | local minedBoard = Instance.new( "IntValue" ) |
056 | minedBoard.Name = "Mined" |
057 | minedBoard.Value = mined |
058 | minedBoard.Parent = leaderstats |
059 | local coinsBoard = Instance.new( "IntValue" ) |
060 | coinsBoard.Name = "Coins" |
061 | coinsBoard.Value = coins |
062 | coinsBoard.Parent = leaderstats |
063 | local rebirthBoard = Instance.new( "IntValue" ) |
064 | rebirthBoard.Name = "Rebirths" |
065 | rebirthBoard.Value = rebirth |
066 | rebirthBoard.Parent = leaderstats |
067 | local rebirthCBoard = Instance.new( "IntValue" ) |
068 | rebirthCBoard.Name = "RebirthCost" |
069 | rebirthCBoard.Value = rebirthC |
070 | rebirthCBoard.Parent = player |
071 | local value = Instance.new( "IntValue" ) |
072 | value.Name = "InventoryValue" |
073 | value.Value = invenVal |
074 | value.Parent = player |
075 | local count = Instance.new( "IntValue" ) |
076 | count.Name = "InventoryCount" |
077 | count.Value = invenCount |
078 | count.Parent = player |
079 | local bpObject = Instance.new( "StringValue" ) |
080 | bpObject.Name = "BackpackItem" |
081 | bpObject.Value = backpack |
082 | bpObject.Parent = player |
083 | local pkObject = Instance.new( "StringValue" ) |
084 | pkObject.Name = "Pickaxe" |
085 | pkObject.Value = pickaxe |
086 | pkObject.Parent = player |
087 | local inMine = Instance.new( "BoolValue" ) |
088 | inMine.Name = "InMine" |
090 | inMine.Parent = player |
091 | onSpawn(player.Character) |
092 | player.CharacterAdded:Connect(onSpawn) |
093 | print ( "Data correctly loaded for " .. player.Name) |
095 | function onPlayerLeaving(player) |
096 | print ( "Starting to save player progress" ) |
097 | local userID = player.UserId |
098 | print ( "Loading Leaderstats" ) |
099 | local stats = player:WaitForChild( "leaderstats" ) |
100 | print ( "Finished Loading" ) |
101 | local mined = stats.Mined.Value |
102 | local coins = stats.Coins.Value |
103 | local rebirth = stats.Rebirths.Value |
104 | local invenVal = player.InventoryValue.Value |
105 | local invenCount = player.InventoryCount.Value |
106 | local backpack = player.BackpackItem.Value |
107 | local pickaxe = player.Pickaxe.Value |
108 | local rebirthC = player.RebirthCost.Value |
109 | local bought = _G.boughtArray [ player.userId ] |
110 | print ( "Blocks Mined: " .. mined .. "Coins: " .. coins) |
111 | local minedKey = "user_" .. player.userId .. "_mined" |
112 | local coinsKey = "user_" .. player.userId .. "_coins" |
113 | local rebirthKey = "user_" .. player.userId .. "_rebirth" |
114 | local rebirthMKey = "user_" .. player.userId .. "_rebirthM" |
115 | local invenValKey = "user_" .. player.userId .. "_invenVal" |
116 | local invenCountKey = "user_" .. player.userId .. "_invenCount" |
117 | local backpackKey = "user_" .. player.userId .. "_backpack" |
118 | local pickaxeKey = "user_" .. player.userId .. "_pickaxe" |
119 | local boughtKey = "user_" .. player.userId .. "_bought" |
120 | local rebirthCKey = "user_" .. player.userId .. "_rebrithC" |
121 | dataStore:SetAsync(minedKey, mined) |
122 | dataStore:SetAsync(coinsKey, coins) |
123 | dataStore:SetAsync(rebirthKey, rebirth) |
124 | dataStore:SetAsync(invenValKey, invenVal) |
125 | dataStore:SetAsync(invenCountKey, invenCount) |
126 | dataStore:SetAsync(backpackKey, backpack) |
127 | dataStore:SetAsync(pickaxeKey, pickaxe) |
128 | dataStore:SetAsync(boughtKey, bought) |
129 | dataStore:SetAsync(rebirthCKey, rebirthC) |
130 | print ( "Player Saved Game" ) |
132 | function onBoughtRequest(player) |
133 | while (_G.boughtArray [ player.userId ] = = nil ) do |
136 | return _G.boughtArray [ player.userId ] |
138 | game.Players.PlayerAdded:Connect(onPlayerEntered) |
139 | game.Players.PlayerRemoving:Connect(onPlayerLeaving) |
140 | boughtRequest.OnServerInvoke = onBoughtRequest |
141 | function Rebirth(player) |
142 | local coins = player.leaderstats:FindFirstChild( "Coins" ) |
143 | local rebirth = player.leaderstats:FindFirstChild( "Rebirths" ) |
144 | local cost = player:FindFirstChild( "RebirthCost" ) |
145 | local invenVal = player:FindFirstChild( "InventoryValue" ) |
146 | local invenCount = player:FindFirstChild( "InventoryCount" ) |
147 | local bpObject = player:FindFirstChild( "BackpackItem" ) |
148 | local pkObject = player:FindFirstChild( "Pickaxe" ) |
149 | local stats = player.leaderstats |
150 | local pickaxe = game.ReplicatedStorage:FindFirstChild( "Basic Pickaxe" ) |
151 | local backpack = game.ReplicatedStorage:FindFirstChild( "BackpackBasic" ) |
152 | local playerGui = player:WaitForChild( "PlayerGui" ) |
153 | local bought = _G.boughtArray |
154 | if coins.Value > = cost.Value then |
156 | cost.Value = cost.Value + 10000000 |
157 | rebirth.Value = rebirth.Value + 1 |
160 | for k in pairs (bought) do |
163 | for _, buttons in pairs (playerGui.MainGui.ShopFrame:GetDescendants()) do |
164 | if buttons:IsA( "TextButton" ) then |
165 | buttons.BackgroundColor 3 = Color 3. fromRGB( 92 , 195 , 37 ) |
168 | bpObject = "BackpackBasic" |
169 | pkObject = "Basic Pickaxe" |
170 | local newItem 1 = pickaxe:Clone() |
171 | local newItem 2 = backpack:Clone() |
172 | player.Character.Humanoid:UnequipTools() |
173 | player:FindFirstChild(player.Pickaxe.Value, true ):Destroy() |
174 | newItem 1. Parent = player.Backpack |
175 | player.Pickaxe.Value = newItem 1. Name |
176 | player.Character:FindFirstChild(player.BackpackItem.Value):Destroy() |
177 | player.Character.Humanoid:AddAccessory(newItem 2 ) |
178 | player.BackpackItem.Value = newItem 2. Name |
179 | capacityEvent:FireClient(player, newItem 2. Capacity.Value) |
184 | game.ReplicatedStorage.Rebirth.OnServerInvoke = Rebirth |