DataStore Sometimes Not Saving?
Asked by
3 years ago Edited 3 years ago
The Datastore I made Was For a Game I am Working On But Sometimes Its Not Saving The Data. I Tried Everything But I Could Find A Way So Thats Why I Am Asking Here. There Is Also A Properties Folder Containing XPPerLevel , AdditionalXPNeeded , LevelCap , XPExponentMagnitude , XPPerLevel.
001 | local Properties = game.ReplicatedStorage.LevelUpProperties |
003 | function onXPChanged(player, XP, level) |
004 | if string.lower(Properties.XPType.Value) = = "linear" then |
005 | if XP.Value> = Properties.XPPerLevel.Value * (level.Value) + Properties.AdditionalXPNeeded.Value and level.Value < Properties.LevelCap.Value then |
006 | XP.Value = XP.Value - (Properties.XPPerLevel.Value*(level.Value) + Properties.AdditionalXPNeeded.Value) |
007 | level.Value = level.Value + 1 |
012 | function SaveData(Player) |
013 | if Player.DataReady = = false then |
014 | print ( "Could not save, DataReady is false." ) |
018 | local Stats = Player.LevelingSystem:GetChildren() |
019 | print ( "Saving Data For " ..Player.Name) |
020 | local datastore = game:GetService( "DataStoreService" ):GetDataStore(Player.Name.. "Stats" ) |
022 | local statstorage = Player:FindFirstChild( "LevelingSystem" ):GetChildren() |
023 | for i , v in pairs (statstorage) do |
024 | datastore:SetAsync(statstorage [ i ] .Name, statstorage [ i ] .Value) |
025 | print ( "saved data number " ..i) |
027 | print ( "Data has been saved successfully for " .. Player.Name .. "!" ) |
030 | function LoadData(Player) |
031 | if Player.DataReady = = false then |
032 | warn( "Could not load, DataReady is false." ) |
036 | local Stats = Player.LevelingSystem:GetChildren() |
037 | local datastore = game:GetService( "DataStoreService" ):GetDataStore(Player.Name.. "Stats" ) |
039 | Player:WaitForChild( "LevelingSystem" ) |
041 | local stats = Player:FindFirstChild( "LevelingSystem" ):GetChildren() |
042 | for i , v in pairs (stats) do |
043 | stats [ i ] .Value = datastore:GetAsync(stats [ i ] .Name) |
044 | print ( "stat number " ..i.. " has been found" ) |
048 | function onPlayerEntered(newPlayer) |
050 | local stats = Instance.new( "Folder" ) |
051 | stats.Name = "LevelingSystem" |
053 | local misc = Instance.new( "Folder" ) |
057 | local clicks = Instance.new( "NumberValue" , stats) |
061 | local clicks 2 = Instance.new( "NumberValue" , stats) |
065 | local clicks 3 = Instance.new( "NumberValue" , stats) |
066 | clicks 3. Name = "Money" |
069 | local clicks 4 = Instance.new( "BoolValue" , stats) |
070 | clicks 4. Name = "EggHunter" |
071 | clicks 4. Value = false |
073 | local clicks 5 = Instance.new( "BoolValue" , stats) |
074 | clicks 5. Name = "PreAlpha" |
075 | clicks 5. Value = false |
077 | local clicks 6 = Instance.new( "StringValue" , stats) |
078 | clicks 6. Name = "Rank" |
081 | local clicks 7 = Instance.new( "NumberValue" , stats) |
082 | clicks 7. Name = "Missions" |
085 | local clicks 8 = Instance.new( "NumberValue" , stats) |
086 | clicks 8. Name = "Time" |
089 | local clicks 9 = Instance.new( "BoolValue" , stats) |
090 | clicks 9. Name = "Banned" |
091 | clicks 9. Value = false |
094 | stats.Parent = newPlayer |
095 | misc.Parent = newPlayer |
097 | clicks 9. Changed:connect( function (newPlayer, clicks 9 ) end ) |
098 | clicks 8. Changed:connect( function (newPlayer, clicks 8 ) end ) |
099 | clicks 7. Changed:connect( function (newPlayer, clicks 7 ) end ) |
100 | clicks 6. Changed:connect( function (newPlayer, clicks 6 ) end ) |
101 | clicks 5. Changed:connect( function (newPlayer, clicks 5 ) end ) |
102 | clicks 4. Changed:connect( function (newPlayer, clicks 4 ) end ) |
103 | clicks 3. Changed:connect( function (newPlayer, clicks 3 ) end ) |
104 | clicks 2. Changed:connect( function () onXPChanged(newPlayer, clicks 2 , clicks) end ) |
105 | clicks.Changed:connect( function (newPlayer, clicks 2 , clicks) end ) |
107 | newPlayer:WaitForDataReady() |
112 | function onPlayerRemoving(player) |
113 | print ( "Attempting to save score for " .. player.Name) |
117 | game.Players.PlayerRemoving:Connect(onPlayerRemoving) |
118 | game.Players.PlayerAdded:Connect(onPlayerEntered) |