Why do I get an error when trying to call the global var

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

I’m trying to post a “Final time” on a victory screen for my game, but when I try to get the script where the variable is held, it gives me this error.
Invalid get index ‘ms’ (on base: ‘null instance’).

extends Label

onready var player_vars = get_node("res://Global.gd")

func _process(delta: float) -> void:
	if player_vars.ms > 9:
		player_vars.s += 1
		player_vars.ms = 0
	set_text(str(player_vars.s) + ":" + str(player_vars.ms))

func _ready() -> void:
	pass # Replace with function body.

func _on_Ms_timeout() -> void:
	player_vars.ms += 1

I put the global script as an autoload.
Here is the script for the global

extends Node

var ms = 0
var s = 0

If you need any more information, I’d be happy to provide

:bust_in_silhouette: Reply From: magicalogic

Your mistake is using get_node() to load your script. Replace that line with this:

onready var player_vars = preload("res://Global.gd").new()

That’s because everything gets reset to default every time the script loads. You might want to save your persistent data in a file.

magicalogic | 2022-11-23 04:41