Skip to content

Instantly share code, notes, and snippets.

@draobrehtom
Created February 22, 2025 03:22
Show Gist options
  • Select an option

  • Save draobrehtom/faefccc3baef316d3a592b2b0141145a to your computer and use it in GitHub Desktop.

Select an option

Save draobrehtom/faefccc3baef316d3a592b2b0141145a to your computer and use it in GitHub Desktop.
OneSync Scope in FiveM/RedM
local playerScopes = {}
local function getPlayersInScope(playerId)
return playerScopes[playerId] or {}
end
local function isPlayerInScope(targetId, forPlayerId)
return playerScopes[forPlayerId] and playerScopes[forPlayerId][targetId]
end
local function initializePlayerScope(playerId)
if not playerScopes[playerId] then
playerScopes[playerId] = {}
end
end
local function handleScopeChange(playerId, forPlayerId, isEntering)
initializePlayerScope(forPlayerId)
local currentlyInScope = isPlayerInScope(playerId, forPlayerId)
if isEntering == currentlyInScope then
return
end
if isEntering then
playerScopes[forPlayerId][playerId] = true
TriggerEvent('optimized:playerEnteredScope', {
['player'] = playerId,
['for'] = forPlayerId
})
else
playerScopes[forPlayerId][playerId] = nil
TriggerEvent('optimized:playerLeftScope', {
['player'] = playerId,
['for'] = forPlayerId
})
end
end
AddEventHandler('playerEnteredScope', function(data)
-- Only process one direction of the scope change to avoid duplicates
if tonumber(data['player']) > tonumber(data['for']) then
handleScopeChange(data['player'], data['for'], true)
end
end)
AddEventHandler('playerLeftScope', function(data)
-- Only process one direction of the scope change to avoid duplicates
if tonumber(data['player']) > tonumber(data['for']) then
handleScopeChange(data['player'], data['for'], false)
end
end)
AddEventHandler('playerDropped', function(playerId)
playerId = tostring(playerId)
playerScopes[playerId] = nil
for _, scopeData in pairs(playerScopes) do
scopeData[playerId] = nil
end
end)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment