Created
February 22, 2025 03:22
-
-
Save draobrehtom/faefccc3baef316d3a592b2b0141145a to your computer and use it in GitHub Desktop.
OneSync Scope in FiveM/RedM
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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