Invalid call. Nonexistent function 'instance' in base 'GDScript'.

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

extends Spatial

var player = preload(“res://Player.gd”)

func _ready():
get_tree().connect(“network_peer_connected”, self, “_player_connected”)
get_tree().connect(“network_peer_disconnected”, self, “_player_disconnected”)

Global.connect("instance_player", self, "_instance_player")


if get_tree().network_peer != null:
	Global.emit_signal("toggle_network_setup", false)

func _instance_player(id):
var player_instance = player.instance()
player_instance.set_network_master(id)
player_instance.name = str(id)

add_child(player_instance)
player_instance.global_transfrom.origin = Vector3(0, 15, 0)

func _player_connected(id):
print(“Player” + str(id) + “has connected”)

_instance_player(id)

func _player_disconnected(id):
print(“Player” + str(id) + “has disconnected”)

if has_node(str(id)):
	get_node(str(id)).queue_free()

what should i do? a iam making 3d sword gmae multiplayer and there is problem when i click host or join this problem shows up Invalid call. Nonexistent function ‘instance’ in base ‘GDScript’.

:bust_in_silhouette: Reply From: SteveSmith

The code

var player = preload("res://Player.gd") 

needs to preload a tscn file (i.e. a scene) not a gd script. Try creating a scene called Player, add Player.gd to it, and change the line to

var player = preload("res://Player.tscn") 
:bust_in_silhouette: Reply From: godot_dev_

If you want to instance a script (as opposed to a scene), you would need to do it as follows:

var playerNode = player.new()
self.add_child(playerNode)

This assumes your player scene is of type Node. If it’s not, then you want to use a scene instead of a script. But in your case, since your using player_instance.global_transfrom.origin = Vector3(0, 15, 0), it implies the player instance is not of type Node, so you probably want to instance the scene like @SteveSmith suggested