Highscore load system not working after I encrypted it [unsolved]

My game without a save file will run perfectly fine, until my player dies, when my player dies, the game crashes with the error below, and if I try to run the game again, it will crash at the main menu with the same error, unless I destroy my save file, then it will be able to run normally again until the player dies (main menu is the main scene)

this used to work fine before I encrypted it, so it might be a obvious mistake.
I used these tutorials:
tutorial part 1
tutorial part 2

My game crashes with this error
image

Main menu script:

extends Control

@export_file("*.tscn") var level: String

var In_level: bool = false
var highscore:int = 0
var death_menu = preload("res://All scenes/Death menu.tscn")

func _ready():
	if not FileAccess.file_exists("user://savegame.save"):
		save_highscore()
	Set_highscore()

func save():
	var save_dict = {
		
		"highscore" = 10
		
	}
	return save_dict

func save_highscore():
	var saved_highscore = FileAccess.open_encrypted_with_pass("user://savegame.save", FileAccess.WRITE, "wadyodo")
	
	var json_string = JSON.stringify(save())
	
	saved_highscore.store_line(json_string) 
func Set_highscore():
	load_data()
	$Highscore.text = str("highscore: ", highscore)


func load_data():
	if not FileAccess.file_exists("user://savegame.save"):
		return
	
	var saved_highscore =  FileAccess.open_encrypted_with_pass("user://savegame.save", FileAccess.READ, "wadyodo")
	
	if FileAccess.file_exists("user://savegame.save"):
		while saved_highscore.get_position() < saved_highscore.get_length(): #erro8r on this line
			var json_string = saved_highscore.get_line()
			var json = JSON.new()
			var parse_result = json.parse(json_string)
			var node_data = json.get_data()
			
			for i in node_data["highscore"]:
				highscore = node_data["highscore"]





func _on_start_game_button_pressed():
	if not In_level:
		get_tree().change_scene_to_file(level)
		In_level = true

func _on_quitgame_pressed():
	get_tree().quit()

game/level/world tree node script:

extends Node2D

var score = 0
var highscore = 0

func _ready():
	var player = get_node("Player")
	player.connect("player_died", on_player_death)


func on_player_death():
	score = $Player.points
	
	var saved_highscore = FileAccess.open_encrypted_with_pass("user://savegame.save", FileAccess.WRITE, "wadyodo")
	
	load_data()
	
	if score > highscore:
		save_highscore()


func save():
	var save_dict = {
		
		"highscore" = score
		
	}
	return save_dict

func save_highscore():
	var saved_highscore = FileAccess.open_encrypted_with_pass("user://savegame.save", FileAccess.WRITE, "wadyodo")
	
	var json_string = JSON.stringify(save())
	
	saved_highscore.store_line(json_string) 

func load_data():
	if not FileAccess.file_exists("user://savegame.save"):
		return
	
	var saved_highscore =  FileAccess.open_encrypted_with_pass("user://savegame.save", FileAccess.READ, "wadyodo")
	
	if FileAccess.file_exists("user://savegame.save"):
		while saved_highscore.get_position() < saved_highscore.get_length(): #erro8r on this line

			var json_string = saved_highscore.get_line()
			var json = JSON.new()
			var parse_result = json.parse(json_string)
			var node_data = json.get_data()
			
			for i in node_data["highscore"]:
				highscore = node_data["highscore"]

btw: I will almost definitely not respond immediately

your save game is good it say it can’t find the player and then change the player data like position see how you handle read from save and you may find that player is not tag good that get_position function can find him maybe in the way you load data ?

also position is not saved in save file also the position is Vector2 and length is float also maybe you can add <= i see you did not save position

<= add this maybe help

1 Like

That actually helped!
Now the error changed to thisimage

	if FileAccess.file_exists("user://savegame.save"):
		while saved_highscore.get_position() <= saved_highscore.get_length(): 
			var json_string = saved_highscore.get_line()
			var json = JSON.new()
			var parse_result = json.parse(json_string)
			var node_data = json.get_data()
			
			for i in node_data["highscore"]: #error_over_here
				highscore = (node_data["highscore"])

For some reason the save data is just … I found this out by using print()

(currently offline)

1 Like

Not sure, why, or how, but after tweaking the system a bit, it works!?