Skip to content

Commit

Permalink
feat(server): triggerClientEvent with multi-target support
Browse files Browse the repository at this point in the history
Since that once fivem PR is forever pending review.
  • Loading branch information
thelindat committed Apr 6, 2024
1 parent 1dd9bd3 commit e476736
Showing 1 changed file with 23 additions and 0 deletions.
23 changes: 23 additions & 0 deletions imports/triggerClientEvent/server.lua
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
---Triggers an event for the given playerIds, sending additional parameters as arguments.\
---Implements functionality from [this pending pull request](https://github.com/citizenfx/fivem/pull/1210) and may be deprecated.
---
---Provides non-neglibible performance gains due to msgpacking all arguments _once_, instead of per-target.
---@param eventName string
---@param targetIds number | number[]
---@param ... any
function lib.triggerClientEvent(eventName, targetIds, ...)
local payload = msgpack.pack_args(...)
local payloadLen = #payload

if type(targetIds) == 'table' and table.type(targetIds) == 'array' then
for i = 1, #targetIds do
TriggerClientEventInternal(eventName, targetIds[i] --[[@as string]], payload, payloadLen)
end

return
end

TriggerClientEventInternal(eventName, targetIds --[[@as string]], payload, payloadLen)
end

return lib.triggerClientEvent

1 comment on commit e476736

@Kenshiin13
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Wow, very nice!!!! How'd you even come up with this?!

Please sign in to comment.