Datastore won't work, and I have no idea why at all? [Solved] [closed]
Here is the whole code which is in one script:
001 | local DataStoreService = game:GetService( "DataStoreService" ) |
002 | local Stats = DataStoreService:GetDataStore( "Stats" ) |
004 | game.Players.PlayerAdded:connect( function (player) |
007 | local info = Instance.new( "StringValue" , game.Workspace.Player_Information) |
008 | info.Name = player.Name |
011 | Instance.new( "StringValue" ,info).Name = "Inventory" |
012 | Instance.new( "StringValue" , info.Inventory).Name = "Gloves" |
013 | Instance.new( "IntValue" , info).Name = "Box_Bux" |
014 | Instance.new( "StringValue" , info).Name = "Wearing" |
015 | Instance.new( "StringValue" , info.Wearing).Name = "Gloves" |
018 | local can_save = Instance.new( "BoolValue" , info) |
019 | can_save.Name = "Can_Save" |
020 | can_save.Value = true |
022 | local stats = game.Lighting.Stats:Clone() |
026 | local success, message = pcall ( function () |
027 | stat_table = Stats:GetAsync(player.UserId) |
032 | if stat_table ~ = nil then |
034 | print (stat_table [ "Gloves_Inventory" ] ) |
035 | print (stat_table [ "Fitness" ] ) |
037 | stats.Strength.Value = stat_table.Strength |
039 | stats.Fitness.Value = stat_table.Fitness |
041 | stats.Endurance.Value = stat_table.Endurance |
043 | stats.Speed.Value = stat_table.Speed |
045 | if stat_table.Box_Bux ~ = nil then |
046 | print ( "loaded properly" ) |
048 | info.Box_Bux.Value = stat_table.Box_Bux |
051 | local gloves_in_inv = stat_table.Gloves_Inventory |
052 | local rounds = string.len(gloves_in_inv)/ 4 |
054 | Instance.new( "StringValue" , info.Inventory.Gloves).Name = string.sub(gloves_in_inv, 1 + ((i - 1 )* 4 ), 4 + ((i - 1 )* 4 )) |
057 | Instance.new( "StringValue" , info.Wearing.Gloves).Name = stat_table.Glove_Wearing |
061 | info.Box_Bux.Value = 0 |
062 | Instance.new( "StringValue" , info.Wearing.Gloves).Name = "G001" |
063 | Instance.new( "StringValue" , info.Inventory.Gloves).Name = "G001" |
068 | stats.Strength.Value = 0 |
070 | stats.Fitness.Value = 0 |
072 | stats.Endurance.Value = 0 |
074 | stats.Speed.Value = 0 |
076 | info.Box_Bux.Value = 0 |
077 | Instance.new( "StringValue" , info.Wearing.Gloves).Name = "G001" |
078 | Instance.new( "StringValue" , info.Inventory.Gloves).Name = "G001" |
081 | can_save.Value = false |
082 | player:Kick( "Your data hasn't loaded correctly, but you haven't lost any data! Please simply rejoin the game!" ) |
086 | game.Players.PlayerRemoving:connect( function (player) |
087 | local player_folder = game.Workspace.Player_Information:FindFirstChild(player.Name) |
089 | if player_folder.Can_Save.Value = = true then |
093 | local strength = player_folder.Stats.Strength.Value |
096 | local fitness = player_folder.Stats.Fitness.Value |
099 | local endurance = player_folder.Stats.Endurance.Value |
102 | local speed = player_folder.Stats.Speed.Value |
105 | local gloves_inv = player_folder.Inventory.Gloves:GetChildren() |
106 | local gloves_owned = { } |
107 | for i = 1 , #gloves_inv do |
108 | gloves_owned [ i ] = gloves_inv [ i ] .Name |
112 | local gloves_wear = player_folder.Wearing.Gloves:GetChildren() |
113 | local gloves_wearing = { } |
114 | for i = 1 , #gloves_inv do |
115 | gloves_wearing [ i ] = gloves_wear [ i ] .Name |
119 | [ "Strength" ] = strength, |
120 | [ "Fitness" ] = fitness, |
121 | [ "Endurance" ] = endurance, |
123 | [ "Gloves_Inventory" ] = table.concat(gloves_owned, "" ), |
124 | [ "Glove_Wearing" ] = table.concat(gloves_wearing, "" ), |
125 | [ "Box_Bux" ] = player_folder.Box_Bux.Value |
128 | print (data_table [ "Gloves_Inventory" ] ) |
129 | print (data_table [ "Fitness" ] ) |
131 | Stats:SetAsync(player.UserId, data_table) |
134 | game.Workspace.Player_Information:FindFirstChild(player.Name):Remove() |
Essentially the code is saving player stats, and clothing etc which is all held in objects in the workspace in a player file. (a folder). I don't think there's anything wrong particularly with what I'm saving, It seems to be how it is saving. When I play the game it's so inconstant with saving, when I play on my own, and then leave, it just doesn't save at all. It saved in 2 player test mode however, so I'm not sure what the issue is. I also added in a script with the bind to close function and a wait of 10, however this appeared to change nothing. I'm not sure how to fix this as there are no errors, and people's data gets seemingly randomly reset.