rpc is not working

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

I am making a multiplayer game with a server and two clients. When I run them they connect to each other but rpc doesn’t work.
server code:

extends Node2D

func _ready():
	var peer = NetworkedMultiplayerENet.new()
	peer.create_server(8765, 2)
	get_tree().network_peer = peer
	
	get_tree().connect("network_peer_connected", self, "_player_connected")

var players=[]

func start():
	get_node("/root").add_child(load("res://play.tscn").instance())
	hide()
	print("game started")

func start_game():
	rpc("start")#this is the thing that doesn't work
	start()

func _player_connected(id):
	players.append(id)
	print(players)
	if len(players)==2:
		start_game()

client code:

extends Node2D

func _ready():
	var peer = NetworkedMultiplayerENet.new()
	peer.create_client("the ip of the server was here", 8765)
	get_tree().network_peer = peer
	
	get_tree().connect("connected_to_server", self, "_connected_ok")

func _connected_ok():
	#this function does get called, so I know that it has connected to the server.
	$CenterContainer/Label.text="waiting for other player"

remote func start():
	#This function should be called by the server when the game starts, but it isn't. 
	print("worked!!!")
	get_node("/root").add_child(load("res://play.tscn").instance())
	hide()

the client’s “start” function does not run when the server calls rpc(“start”) even though the server and client are connected.

I also get these errors on the client even thought the node path on the client and server are the same:
Invalid packet received. Unabled to find requested cached node.
Invalid packet received. Requested node was not found.

why is the client’s start function not being called when the server calls rpc(“start”) ?

Is the node structure of the scene the same on both the server and the client?

RedBlueCarrots | 2020-12-03 09:36

the script is attached to /root/connecting on both the server and clients but underneath that they are different

jzzzzz | 2020-12-04 05:54

Yeah, in order for rpc to work it must be on a node with the same name / heirachy above it

RedBlueCarrots | 2020-12-05 05:19

the node with the script does have the same name and node path on the client and sever

jzzzzz | 2020-12-06 16:26

I don’t see anything telling it to call start_game()

Merlin1846 | 2021-04-19 14:22

it does in the last line of the server code,
and I have made sure it is being called by making it print something, so I know that works.

jzzzzz | 2021-04-19 18:18