Nearby players
Returns all player source IDs within a radius of a reference player. Pure FiveM natives — framework-agnostic.
GetPlayersInRadius(source, radius) → tableserver
Returns an array of server source IDs for all players within radius metres of source. Never includes source itself. Returns an empty table if no other players are in range.
| Param | Type | Description |
|---|---|---|
| source | number | The reference player's server source ID. |
| radius | number | Search radius in metres. |
example
local nearby = exports.wc_libs:GetPlayersInRadius(source, 50.0)
for _, pid in ipairs(nearby) do
TriggerClientEvent('myResource:doThing', pid)
end
Real Wild County example — stagecoach breakdown call for help
wc_stagecoach notifies nearby players when a wagon breaks down so they can offer to help, without broadcasting to the entire server.
wc_stagecoach/server/main.lua
local nearby = wc:GetPlayersInRadius(src, 75.0)
for _, pid in ipairs(nearby) do
wc:WcNotify(pid, 'A stagecoach broke down nearby.', 'INFO')
end
Common mistakes
- Calling
GetPlayersInRadiusevery frame in a loop — it iterates every connected player each call; run it on an interval (e.g. every second) or on-demand, not per-tick. - Forgetting the result never includes
sourceitself — don't add an extra check to exclude it, it's already excluded. - Using this for anti-cheat-sensitive distance checks without also validating server-side that the player's reported position is plausible.
Troubleshooting
- If the returned list is always empty: double check the radius units — this is metres, and RedM's map is large; 75–150 is a reasonable "nearby" range for most interactions.
- If it includes players who feel too far away: remember it's a 3D distance including vertical (Z) — a player on a cliff above/below is included if within the spherical radius.