How to replicate on press space key event to all clients via cpp code

A()
{
if(pressed(space))
print “hiii”
print “hello”
}

tick()
{
A()
}

"Hello"is printed always but “hiii” is printed only on space key press. I want to replicate that to all clients so that “hiiii” gets printed on all clients and server when i press space key on server.

You need 2 methods :

.h
        UFUNCTION( Server )
        void ServerPrintHiToClients();
    
        UFUNCTION( NetMulticast )
        void MulticastPrintHi();

.cpp
Class::ServerPrintHiToClients_Implementation()
    {
        MulticastPrintHi();
    };
Class::MulticastPrintHi_Implementation()
    {
           print("hiii");
    };

your code become :

A() { if(pressed(space)) ServerPrintHiToClients(); }

TIck(){A();}
tick() { A() }
explanation :

Client call localy “ServerPrintHiToClients”

UE send message from client to server

Server execute “ServerPrintHiToClients_Implementation”

Server call locally " MulticastPrintHi"

Server send message to all clients

Clients executes “MulticastPrintHi_Implementation”

i suggest you to have a look here :