How do I make it so players can't edit the file but the game can? File.CreateText

So my high score system is a little weird. I use the File.CreateText thing to do it, here is the script if you need it: (Java and its called ScoreSave)

 import System.IO;

 var fileName = "hs.data";
 var ScoreAmount : int;
 
 var HighScore : int;
 
 function Start (){
     HighScore = ScoreLoad.CompareScore;
     ScoreAmount = GlobalScore.CurrentScore;
     if (ScoreAmount >= HighScore){
         var OurFile = File.CreateText(fileName);
         OurFile.WriteLine (ScoreAmount);
         OurFile.Close();
     }
 }

Also here is my script for loading the score if that is needed for explanation (java and its called ScoreLoad):

     import System.IO;
     
     var fileName = "HS.data";
     var ScoreLoad : String;
     var HighScoreDisplay : GameObject;
     
     var line : String;
     
     static var CompareScore : int;
     
     function Start(){
         var sr : StreamReader = new StreamReader(fileName);
     
         line = sr.ReadLine();
         while (line != null){
             ScoreLoad = line;
             line = sr.ReadLine();
     
         }
         sr.Close();
     
         HighScoreDisplay.GetComponent.<Text>().text = "" + ScoreLoad;
     
       

  CompareScore = int.Parse(ScoreLoad);
 }

My question is how do I make it so in the final build, players wont just be able to go to the game files, go into the file called hs.data and change their high score to a high number aka cheat? How do I like encrypt it or lock it or make it permanently read only or something? Anything helps, thank you! Also it needs to be able to save the highscore when it gets a new one so would read only not work? Help please. Thanks.

I see two ways of doing it. Either set the Read/Write access of the file to something secure, i.e.

File.SetAttributes (fileName, FileAttributes.Hidden);
File.SetAttributes (fileName, FileAttributes.ReadOnly);

Or, you could use a binary formatter to convert the text to binary; unreadable and unchangeable for anything but the system that encoded it.

  import System.IO;
  import System.Runtime.Serialization.Formatters.Binary;    //I know, it's a bit long eh? 

  var fileName = "hs.data";
  var ScoreAmount : int;
  
  var HighScore : int;
  
  function Start (){
      HighScore = ScoreLoad.CompareScore;
      ScoreAmount = GlobalScore.CurrentScore;
      if (ScoreAmount >= HighScore){
          var bf = new BinaryFormatter ();
          var OurFile = File.Open (fileName, FileMode.OpenOrCreate);
          bf.Serialize (OurFile, ScoreAmount);
          OurFile.Close();
      }
  }

...

//Then for loading the file;

var bf = new BinaryFormatter ();
var OurFile = File.Open (fileName, FileMode.Open);
line = bf.Deserialize (OurFile).ToString();
OurFile.Close ();

There might be an error in the “bf.Deserialize()” line, simply because it’s been a while since I did deserialization in JS, so if you use this method and have difficulties just leave a comment.

not an answer, just an observation as I “struggled” with this very same thing. But in the end I said “who cares” . Games are meant to be fun and played as designed so if some snot nosed punk wants to cheat and pad his score, by all means have at it. Unless you’re making a AAA title for massive online play or there is some tangible reward for getting a high sore like bit coins I say let them hack it. Let them have that rush of pseudo superiority