How do I create a file only on the first launch of the app using the file storage?

Hello, I'm new to Mit app inventor and I wanted to make a clicker game, but I have a problem with saving the data. I'm using the "File" to store the score so that when the player goes to the shop which is in another screen he can still see the points he got and when he spends some and go back to the main screen it saves it and sends the score to the main screen. The problem I have is that firstly you have to create this file so I made a varible and set it to true, when the main screen (Screen1) initalizes and if the varible is true then make the file and then set the varible to false so that when it initalizes the time it goes back from the shop it just reads the data, but the problem with this is that the varible doesn't wanna change to false and the result I get is that when i go back from the shop it always puts an extra 0 at the end of the number so when I have 5 points and go to the shop its still 5 but when i come back its 50 when i got to the shop again its 50 and when I come back its 500 and so on. Can you please help me?

Screen 1:

Screen 2 (shop):

Instead of using a file and writing the results to it, I would advise you to use TinyDb for this. Someone can easily hack the file and enter any number of scores in it.

Thanks for the reply, I know that someone can easily change the file since thats what i thought when doing this and checked it and yea but it's only ment to be for my friends/family so that would be alright. I tried with TinyDB eaelier but the problem with this is that it's working perfecly fine while connecting and testing it but when i build the .apk and download it on my phone it shows an error.

It is not possible. You are probably using screens wrong, switching them wrong. Then there are problems.

I'll do it with TinyDB and send you a screenshot of the error I get, just give me few minutes.

Use this method to switch screens.

Where Screen1 is, you can add whatever screen you want to open.

Thanks, I'll try it in a sec, but there's still a problem cause now it will not work because when i open the screen it shows an error because theres nothing saved in the tinyDB yet and I have to have it here so that when i come back from screen 2 to screen1 i need it to get the data from the tinyDB

Why did you add "Error" there? Add "0" there. Then you will have 0 the first time you run it. Read the documentation on TinyDb.

Thanks it work, I'm super greatful for your help, I was struggling with this problem for past few months and that's why i took a break from MIt app inventor, thanks for a quick response too.

You're welcome. And change the way you open screens. Because the way you presented is not correct. As soon as you open screens without closing them, at some point the application will crash due to insufficient memory. Imagine that all the screens that you open several times are opened even when you cannot see them.

1 Like