Why am i getting this error message [Godot3.0]

:information_source: Attention Topic was automatically imported from the old Question2Answer platform.
:bust_in_silhouette: Asked By Taabta

hello, i wanted to create a system to save levels progress. i found a script posted by someone that does the thing, but whenever i save the game and get to load it it shows me this error message:

Invalid get index ‘level’ (on base: ‘Nil’)

Here is the script:

extends Node

var currentLevel



func _ready():
	
	
	var dir = Directory.new()
	if !dir.dir_exists("user://Saves"):
		dir.open("user://")
		dir.make_dir("user://Saves")


func get_level():
	return get_tree().get_current_scene().get_filename()


func set_level(var levelName):
	
	currentLevel = levelName
	get_tree().change_scene(levelName)


var GameData = {
	level=""
	}


func save_game_state(var saveName):
	
	
	var saveGame = File.new()
	saveGame.open("user://Saves/"+saveName+".sve", File.WRITE)
	
	
	var data = GameData
	
	
	
	
	data.level = get_level()
	
	
	
	saveGame.store_line(to_json(data))
	saveGame.close()


func load_game_state(var saveName):
	
	
	var loadGame = File.new()
	
	
	if !loadGame.file_exists("user://Saves/"+saveName+".sve"):
		print ("File not found! Aborting...")
		return
	
	
	var currentLine = {}

	
	loadGame.open("user://Saves/"+saveName+".sve", File.READ)
	while(!loadGame.eof_reached()):
		
		
		currentLine = parse_json(loadGame.get_line())
		
		
		currentLevel  =  currentLine["level"]
	loadGame.close()
		
	

func new_game():
	
	currentLevel = "res://level1.tscn"
	set_level(currentLevel)

Note: I edited your post to fix the code formatting. In future, please either put 4 spaces in front, or click the “Code Sample” button to format when posting.

kidscancode | 2019-04-26 04:16

You didn’t say what line the error was on…

kidscancode | 2019-04-26 04:19

From what I see, it means GameData became null, but can’t say why. Something else in your project might be modifying it.

Zylann | 2019-04-26 13:02