title: How can I fix the data in roblox
body: <p>I asked a second question because my last one was getting out of the subject with comments. So I'm trying to make roblox saving the stats of a player but it puts always the basic stats. Even if the player already played.
  Here's my code :</p>
  
  <pre><code>local HttpService = game:GetService("HttpService")
  local DataStoreService = game:GetService("DataStoreService")
  local ds = DataStoreService:GetDataStore("Data")
  
  game.Players.PlayerAdded:Connect(function(player)
      local defaultData = {
          coins = 0,
          gems = 0,
          xp = 0,
          level = 1,
          maxstamina = 100,
          maxmagic = 100,
      }
      local loadedData = defaultData
      local success, result = pcall(function()
           return ds:GetAsync(player.UserId)
      end)
      if success then
          if result then
              -- if we've successfully loaded the data, parse the stored json data
              print(string.format("An old player named %s has returned with data : %s!", player.Name, tostring(result)))
              -- player data should look like this :
              -- {"coins":0,"xp":0,"gems":0,"level":1,"maxstamina":100,"maxmagic":100}
              local parseSuccess, parseResult = pcall(function()
                  return HttpService:JSONDecode(result)
              end)
              if parseSuccess then
                  loadedData = parseResult
              else
                  warn(string.format("Failed to parse %s with error : %s", tostring(result), tostring(parseResult))) 
              end
          else
              -- we have a new player
              print(string.format("New player named %s has joined!", player.Name))
          end
      else
          warn(string.format("Something went wrong fetching %s's data : %s", player.Name, tostring(result)))
          -- TO DO: FIGURE OUT HOW YOU WANT TO HANDLE THIS ERROR.
          -- IF YOU DO NOTHING, THE PLAYER'S DATA WILL BE THE DEFAULT DATA USED FOR
          -- NEW PLAYERS
      end
  
      -- create the leaderstats and hidden values, load the data from the loadedData table
      local leaderstats = Instance.new("Model", player)
      leaderstats.Name = "leaderstats"
  
      local hidden = Instance.new("Model", player)
      hidden.Name = "hidden"
  
      local coins = Instance.new("IntValue", leaderstats)
      coins.Name = "Coins"
      coins.Value = loadedData.coins
  
      local gems = Instance.new("IntValue", leaderstats)
      gems.Name = "Gems"
      gems.Value = loadedData.gems
  
      local level = Instance.new("IntValue", leaderstats)
      level.Name = "Level"
      level.Value = loadedData.level
  
      local xp = Instance.new("IntValue", hidden)
      xp.Name = "XP"
      xp.Value = loadedData.xp
  
      local maxstamina = Instance.new("IntValue", hidden)
      maxstamina.Name = "MaxStamina"
      maxstamina.Value = loadedData.maxstamina
  
      local maxmagic = Instance.new("IntValue", hidden)
      maxmagic.Name = "MaxMagic"
      maxmagic.Value = loadedData.maxmagic
  
      local stamina = Instance.new("IntValue", hidden)
      stamina.Name = "Stamina"
      stamina.Value = loadedData.maxstamina
  
      local magic = Instance.new("IntValue", hidden)
      magic.Name = "Magic"
      magic.Value = loadedData.maxmagic
  end)
  
  game.Players.PlayerRemoving:Connect(function(player)
      local stats = player.leaderstats
      local hidden = player.hidden
  
      local data = {
          coins = stats.Coins.Value,
          gems = stats.Gems.Value,
          xp = hidden.XP.Value,
          level = stats.Level.Value,
          maxstamina = hidden.MaxStamina.Value,
          maxmagic = hidden.MaxMagic.Value,
      }
  
      -- wrap the request in a try-catch block to ensure that failures don't throw errors
      local success, result = pcall(function()
          -- save all the data as a JSON string
          ds:setAsync(player.UserId, HttpService:JSONEncode(data))
      end)
      if not success then
          warn(string.format("Failed to save %s's data with error : %s", player.Name, tostring(result)))
          -- TO DO: FIGURE OUT HOW YOU WANT TO HANDLE THIS ERROR.
          -- IF YOU DO NOTHING, THE PLAYER WILL LOSE THIS SESSION'S DATA
      end 
  end)
  </code></pre>
  
  <p>More info : </p>
  
  <ul>
  <li><p>The game is public but not finished here's the link if you wan't to test it : <a href="https://web.roblox.com/games/4867142155/Medieval-Fighting-Simulator-Beta?refPageId=b25ecb6b-40c9-4c28-a4bb-2e4e0a6c6d61" rel="nofollow noreferrer">https://web.roblox.com/games/4867142155/Medieval-Fighting-Simulator-Beta?refPageId=b25ecb6b-40c9-4c28-a4bb-2e4e0a6c6d61</a></p></li>
  <li><p>The Studio API Service is enabled</p></li>
  <li><p>There's no error code</p></li>
  </ul>
  
  <p>How can you help me?</p>