Skip to content

feat(server): support a list of players in TriggerClientEvent - #4166

Open
spacevx wants to merge 2 commits into
citizenfx:masterfrom
spacevx:feat/event-targets
Open

spacevx wants to merge 2 commits into
citizenfx:masterfrom
spacevx:feat/event-targets

Conversation

@spacevx

@spacevx spacevx commented Sep 2, 2026

Copy link
Copy Markdown
Contributor

Goal of this PR

Allow TriggerClientEvent on the server to send an event to a list of players in one call, instead of looping and calling it once per player.

Based on the PR of @PichotM #1210

How is this PR achieving the goal

I added a new native, TRIGGER_MULTICAST_CLIENT_EVENT_INTERNAL (i didn't want to edit TriggerClientEventInternal, so i made a new one) that takes the same event name and payload as TRIGGER_CLIENT_EVENT_INTERNAL plus the targets as a msgpack array of net ids. The packet is built once, the list is deduplicated so a player never receives the event twice, and the packet is sent to every connected client of the list. The packet building of the existing TriggerClientEvent was moved to a shared function so both paths produce the exact same bytes, the scalar path itself is unchanged.

local players = { 4, 5, 6 }
TriggerClientEvent("sendmsg", players, "Hello everyone")

-- You can also do
local players = { "4", "5", "6" }
TriggerClientEvent("sendmsg", players, "Hello everyone")

This PR applies to the following area(s)

Server, Natives, ScRT: Lua, ScRT: JS, ScRT: C#

Successfully tested on

Platforms: Windows

Checklist

  • Code compiles and has been tested successfully.
  • Code explains itself well and/or is documented.
  • My commit message explains what the changes do and what they are for.
  • No extra compilation warnings are added by these changes.

Fixes issues

None

@github-actions github-actions Bot added ScRT: C# Issues/PRs related to either C# scripting runtimes ScRT: JS Issues/PRs related to the JavaScript scripting runtime ScRT: Lua Issues/PRs related to the Lua scripting runtime triage Needs a preliminary assessment to determine the urgency and required action labels Sep 2, 2026
@AvarianKnight

Copy link
Copy Markdown
Contributor

This is a great change, but I don't think we should add this onto existing paths, we should just make new wrapper functions for the multicast variant.

@spacevx

spacevx commented Sep 2, 2026

Copy link
Copy Markdown
Contributor Author

This is a great change, but I don't think we should add this onto existing paths, we should just make new wrapper functions for the multicast variant.

I did it like this, because you said: "From a general perspective adding more functions might be more confusing than just overloading TriggerClientEvent, especially when they copy the same behavior"

And for me too it's better to not recreate another native, but if everyone prefer a new native, that's okay for me x)

@spacevx

spacevx commented Sep 2, 2026

Copy link
Copy Markdown
Contributor Author

@FabianTerhorst What's your opinion on this?

@manups4e

manups4e commented Sep 16, 2026

Copy link
Copy Markdown
Contributor

Maybe i missed that part in the code.. but what if i send the event to a list with server id repeated such as {1, 1, 2, 2, 3, 3, 3, 3}?
Edit: Found it! 😄 seems nice to me.. especially in the .net part

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

ScRT: C# Issues/PRs related to either C# scripting runtimes ScRT: JS Issues/PRs related to the JavaScript scripting runtime ScRT: Lua Issues/PRs related to the Lua scripting runtime triage Needs a preliminary assessment to determine the urgency and required action

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants