I am currently making a multiplayer game. I am currently on the lobby, where I have stumbled upon a problem that I cannot solve. I have been searching for some kind of clearer explanation of [ClientRPC] and [Command], but I just can’t seem to grab a hold of it.

Note: I am working on a local multiplayer game, I am not using the matchmaker

The problem is, I am currently working on the lobby, where the players can join, and upon, joining, the PlayerLobby will load a string and an image. It works fine, except, when a client connects to the lobby, it will load the same string and image as the server. I am currently at a loss on what to do, and I am unsure if my approach toward this multiplayer lobby is correct.

This is my current approach:
upon conection to the lobby,

// if server, load data using ClientRpc
    if(isServer) {
    	RpcLoadData ();
    }

// if not the server (client), tell the server to load
    else {
    	CmdLoadData ();
    }

And this is the actual code of the Command and ClientRpc

    [Command]
    public void CmdLoadData() {
    	RpcLoadData ();
    }
    
    [ClientRpc]
    public void RpcLoadData() {
    	BinaryFormatter bf = new BinaryFormatter();
    	FileStream playerFile = File.Open(Application.persistentDataPath + "/" + playerFileName, FileMode.Open);
    	playerData = (PlayerData)bf.Deserialize(playerFile);
    	monsterData = playerData.getCurrentMonster ();
    
    	OnMyPlayerName(playerData.getPlayerName ());
    	OnMyMonsterName(playerData.getCurrentMonster ().getMonsterName ());
    	OnMyPortrait(MonsterType.IMAGE[monsterData.getMonsterTypeID()]);
    
    	playerFile.Close();
    }

Thanks for any help in advance!

Edit:
I updated the code that I have posted, to have an accurate information of what I am actually doing.

[Command] runs the function on the server using data on the client. [ClientRpc] runs the function on all clients using data from the server.
Atleast thats the basic idea I’ve got.

I think what you’re doing doesn’t work because when you run your RpcLoadData() the function is run at the server, and thus uses the server variables (which would be the same as Player 1).
Just use an identical function but just make it a [Command] instead (tell the server to run this function, but with the data from the client) or pass all your local variables via the [Command] to the [ClientRpc] function.

@nyaw
Hey. I have the same problem with you.
How do you solve it?

My case is that I have two players to shoot arrow. The arrow speed is depends on “Local Player” charge time. I wonder if I can send the localplayer data to server?