How to replicate an event to a specific client only?

Hi,

I have a treasure chest where, if opened by a specific pawn, I want that specific pawn to only see the chest opening animation but other nearby clients including the server should not? I still want the server to give the client the items to prevent the cheating but visually-wise, the player on the server shouldn’t know that the client opened the chest.

Thanks!

1 Like

Client that you only want to replicate within an event can pass PlayerState.PlayerID into it via Server call. Then within the Server event, check if the passed PlayerID matches the current PlayerController’s PlayerID. If so, then perform you want to replicate, otherwise, do nothing.

Below is my example:

  1. Create a GetState (returns PlayerState) function inside your PlayerController.

266936-e1-getstate-function.png

  1. Create GetPlayerID function inside your PlayerController.

  1. Create IsCurrentPlayer function inside you PlayerController.

  1. On your Server event, compare the passed object’s PlayerID with the current PlayerID. If they match, then it means that the current player evaluating the event is the same as the one who called it via Server RPC call.
    Also, if you want to replicate it to other clients except the caller of the event, just use the False connector instead of the True one.

Now that you know how to do it the hard way, here’s the magic way of doing it:

  1. Pass the Client’s PlayerController that called the event via ServerRPC. Inside the event, drag the connector from the passed PlayerController input and then use the IsLocalController or IsLocalPlayerController node. True means that the current player evaluating the event is the same as the one who called it via Server RPC call.

2 Likes

Wow it worked! Thanks!

Hm… sorry, but I think this doesn’t really “replicate an event to a specific client only”. Instead, it “replicates an event to all clients and all except one is doing nothing upon receiving the event”. So, does anyone know how to do it properly in unreal?

1 Like

If you Spawn a Replicated Actor set to “Only Relevant to Owner” and while spawning it setting the owner to a specific Player then only this player will receive any updates to this Actor.

The PlayerController works like this and prevents anyone else from getting any updates besides the Owner and the Server.

2 Likes