How to fire an event to a client

Hello, I wanted to know if it was at all possible to fire an event from a server script to a client.

The task I am trying to accomplish is creating an orb that works as a zone and whoever is hit by that zone, gets a script parented to them to which then the server script sends that script that got parented a keyword to make some particle effect appear but for anybody OUTSIDE the zone, there are 0 particle effects.

How would I go about using a remote event that’s Server > Client via hitbox/zone?

Any and all help is appreciated.

My code so that it makes a little more sense:

		local VoidZone = Zone.new(Dome)
		local DomainLocalScript = game.ReplicatedStorage.Domain.LocalDomainEvent:Clone()
		
		VoidZone.playerEntered:Connect(function(player)
			print(player)
			DomainLocalScript.Parent = player
			task.wait()
			DomainEvent:OnClientEvent("Expand")
		end)
		
		task.wait(0.1)
		VoidZone:Destroy()
		print("Zone Deleted")

Once again, thank you if you took the time to read this.

4 Likes

RemoteEvents were literally forged by the gods and placed on this disgusting mortal realm for this purpose, use FireClient() on the server side and listen with OnClientEvent on the client side

EDIT: I’m silly and didn’t read what you were wanting, lol. Just check who is in the zone with something like GetPartBoundsInBox and fire to only those clients (FireClient takes a player parameter)

3 Likes

Like @Negativize said, you could do something like

YourRemoteEvent:FireClient(PlayerInstance, OtherOptionalParameters)

Your client script would look like

YourRemoteEvent.OnClientEvent:Connect(function(OtherOptionalParameters)
--stuff
end)
4 Likes

I’d also like to clarify that I replaced “OnClientEvent” for “FireClient”, I had that inverted but it still doesn’t work.

I’d also like to add that output says: “Unable to cast value to Object” on the line of “DomainEvent:OnClientEvent(“Expand”)”

2 Likes

The best approach in my opinion is not to clone a script to the Client, but rather to have the Client receive a signal using .OnClientEvent within a local script and firing the Client using RemoteEvents located somewhere in ReplicatedStorage.

Then firing the event upon Zone entry from the Server like so:

RemoteEvent:FireClient(player, ...)

Then the Client receives it and performs particle effects upon fired event

RemoteEvent.OnClientEvent:Connect(function()
     -- Particle Effects here
end)
2 Likes

I see, I’ll have to research that then

I still would have to include my “keyword” for a lack of a better term, right?

You can view these documentation pages for a better understanding of how client-server runtime and Remote Events work:

2 Likes

Where would that local script be exactly?

Thank you, I’ll open this right now

What do you mean by keyword? I looked over the original post but i dont understand what you mean because i am stupid.

1 Like

The local script belongs within StarterPlayerScripts, but more properly StarterCharacterScripts because the particle effects will ostensibly be shown multiple times (respawning).

1 Like

To use remote events, don’t you need to use like a type of word that talks to said script that it receives and uses?

Such as:

Client Script:

DomainEvent:FireServer("DomainExpansion")

Server Script:

if Key == "DomainExpansion" then

No, you don’t need to do that. The parameters in :FireServer() are all optional

1 Like

Wait, so how would the server script know what to do with my remote event?

I am kind of confused, is DomainExpansion the name of the zone?

You need not do this, but to utilize the Remote Event for multiple functions such as an “effect” event, you may want to tie the event to multiple things, and pass along a string in order to differentiate which effect you would like to use.

When firing clients, make sure to pass in the Player as the first argument.
When firing the Server, the first argument is whatever you pass in.

2 Likes

No, “DomainExpansion” is the “key” I set in the client script so that when it communicates Client > Server, Server knows what to do when it sees “DomainExpansion”

More In depth code

Local:

if input.UserInputType == Enum.UserInputType.MouseButton1 and DomainActive == true and Debounce == false then
		Debounce = true
		DomainEvent:FireServer("DomainExpansion")
		Anim.AnimationId = Activate
		local LiveAnim = Character.Humanoid:LoadAnimation(Anim)
		LiveAnim:Play()
		task.wait(10)
		Debounce = false
	end
end)

Server:

DomainEvent.OnServerEvent:Connect(function(Player, Key)
	if Key == "DomainExpansion" then

Instead of using a key, you can just create separate RemoteEvents

1 Like

Wouldn’t that be harder on the game as a whole? Or is running it all through 1 remote even worse