Invalid get index 'count' (on base Nil)

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

i’ve been trying to follow a tutorial for making a clicker game and my code looks identical to the one shown in the video, and im trying to store the count variable to a file. (The full script in the video can be seen at ~26 minutes in.)
When I try to run it it closes immediately and tells me “Invalid get index ‘count’ (on base Nil) ,” and it points out the following line specifically.

count = data.count

The following is the full script.

extends Node

var autoclicker = 0
export var count = 0

func _ready():
	var file = File.new()
	if file.file_exists("user://save.txt"):
		file.open("user://save.txt", File.READ)
		var data = parse_json(file.get_line())
		count = data.count

func _on_Make_a_Thing_pressed():
	count +=  1

func _process(delta):
	$Thngs.text = str(count)

func _on_SavingTimer_timeout():
	var data = {
			count = count
		}
	var file = File.new()
	file.open("user://save.txt", File.WRITE)
	file.store_line(to_json(data))
	file.close()

Any help is much appreciated!

:bust_in_silhouette: Reply From: samjmiller

Usually “Invalid get index” means that you’ve incorrectly called something - either because there’s a slight misspelling or because the object you’re calling does not exist.

“on base Nil” indicates that it can’t find ‘data’ - can you confirm that everything in this line of code

var data = parse_json(file.get_line())

is properly entered, and consistent from previous usages?

You can try calling

print(data, count, file.get_line())

or

print("data is ", data, "count is ", count, "file.get_line() is ", file.get_line())

instead of the line of code "count = data.count", and then checking the console - what’s coming up for data? what’s coming up for count? file.get_line()? that might help you see where the problem might be.