How would I properly use OnClientEvent?

I am trying to use OnClientEvent and FireClient, but it keeps popping up with errors. One of which is (OnClientEvent can only be used on the client) and (FireClient can only be called from the server). I am using a RemoteEvent. It seems to work when I replace FireClient with FireServer and replacing OnClientEvent with OnServerEvent. Here are some pictures.

5hww45jhtjwawbuy Script
devfourmhelpscripting LocalScript5;7lraebgFVWCD Game

OnClientEvent can only be used on the client, as it is a listener for FireClient, which can only be called form the server.

Thus, OnClientEvent and FireClient are used in tandem for communication from the server to clients.

OnServerEvent is a listener for FireServer. OnServerEvent can only be used on the server, and FireServer can only be called from the client.

OnServerEvent and FireServer are used together for communication from clients to the server.

3 Likes

Its still extremely confusing, and I have been trying to work it out using RemoteEvent | Documentation - Roblox Creator Hub
but it is still very confusing. Ive used both OnClientEvent and FireClient at the same time but it still pops up with an error saying “OnClientEvent can only be used on the client”

1 Like

“Can only be used on the client” means that it can only be used in a LocalScript, not in a regular script. Local scripts only run on the client as descendants of certain objects, such as the PlayerGui.

This article explains the difference between clients, servers, and talks about client-server communication:

This reference page gives more information about LocalScripts then I gave in this post:

I switched it to a LocalScript, and now its even more broken. The youtube videos ive watched about this leave it on a Script, and they have no problems. Ive been trying to replicate this, but ive failed.

Put that LocalScript in StarterCharacterScripts and in the Script, instead of ‘Remote.OnClientEvent:Connect(player)’ do ‘Remote.OnServerEvent:Connect(player)’.

In roblox there is a Server and a Client. A server is the actual game server and the client is the actual player.

A script cannot listen to OnClientEvent because it is not the player, instead, we made this to OnServerEvent. Now, to make that function fire on the script, we will need to use Remote:FireServer(arguments) in a local script.

What YouTube video uses OnClientEvent in a server script? That does not work.

Have you actually read the links I provided in my previous post?

Yes. I have read it, and I changed the Script to LocalScript. It didn’t work out, so then I said that Youtube videos i’ve watched did it that way.

You fire the RemoteEvent from the server using RemoteEvent:FireClient(player, ...) and in a local script running on the client, you can use RemoteEvent.OnClientEvent:Connect(function). ... are the arguments you wish to send to the client. If you want the client to send info to the server, use :FireServer and .OnServerEvent

1 Like