My variable is causing an error when I print it from another script

Godot Version

v4.2.2

Question

I’m making a game where the user first choices single or multiplayer and then a category. My idea is to use a variable and assign it a value of 1 if it is singleplayer and a value of 2 if its multiplayer. I made a global variable in one of my scripts and when I print that variable it works fine and prints it out. However, when I call that same variable in another script it causes an error. It specifically says, “Invalid get index ‘SingleOrMulti’ (on base: ‘Node (PlayerOptions.gd)’)”

This is the PlayerOption code (Single or Multiplayer):
extends Node

Called when the node enters the scene tree for the first time.

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

Called every frame. ‘delta’ is the elapsed time since the previous frame.

func _process(_delta):
pass

func _on_button_for_single_pressed():
var SingleOrMulti = 1
print(“Single player selected. SingleOrMulti:”, SingleOrMulti)
get_tree().change_scene_to_file(“res://CategoryMenu.tscn”)
pass # Replace with function body.

func _on_button_for_multi_pressed():
var SingleOrMulti = 2
print(“Multi player selected. SingleOrMulti:”, SingleOrMulti)
get_tree().change_scene_to_file(“res://CategoryMenu.tscn”)
pass # Replace with function body

func _on_button_for_back_pressed():
get_tree().change_scene_to_file(“res://MainMenu.tscn”)
pass # Replace with function body.

All of this seems to work fine but in my category menu is where I get the error here is the code for that:

extends Control

Called when the node enters the scene tree for the first time.

func _ready():
pass

func SingleOrMultiPlayer():
print(“SingleOrMultiPlayer function called.”)
print(PlayerOptions.SingleOrMulti)
if PlayerOptions.SingleOrMulti == 1:
print(PlayerOptions.SingleOrMulti)
get_tree().change_scene_to_file(“res://SinglePlayerGame.tscn”)
elif PlayerOptions.SingleOrMulti == 2:
print(PlayerOptions.SingleOrMulti)
get_tree().change_scene_to_file(“res://MultiPlayerGame.tscn”)
else:
print(“test”)
pass

Called every frame. ‘delta’ is the elapsed time since the previous frame.

func _process(_delta):
pass

func _on_button_for_back_pressed():
get_tree().change_scene_to_file(“res://PlayerOptions.tscn”)
pass # Replace with function body.

func _on_button_for_countries_pressed():
var _CategoryChoice = 2
SingleOrMultiPlayer()
pass # Replace with function body.

When I run the SingleOrMultiPlayer() function what happens is that it runs the first line of it but then the error appears after it and nothing is printed.

Hard to tell without proper code formatting, but it looks like you made SingleOrMulti a local variable.

func _on_button_for_multi_pressed() -> void:
    # will not exist outside of this function
    var SingleOrMulti = 2

You could also check if multiplayer.multiplayer_peer == null, or better yet write the same code for multiplayer but assign a OfflineMultiplayerPeer

func _on_button_for_single_pressed() -> void:
	var peer := OfflineMultiplayerPeer.new()
	multiplayer.multiplayer_peer = peer

Use the </> button on a new line to paste code like so

```
type or paste code here
```