RSGCore.Functions.GetPlayerData

  • Perhaps the most used function in the framework. This function returns the players data of the current source which, since its used client side, is automatically the client or player. It can be used with modifiers on the end starting with a ”.” (period)
function RSGCore.Functions.GetPlayerData(cb)
    if not cb then return RSGCore.PlayerData end
    cb(RSGCore.PlayerData)
end

-- Example

local Player = RSGCore.Functions.GetPlayerData()
print(RSGCore.Debug(Player))

OR

local Player = RSGCore.Functions.GetPlayerData()
local jobName = Player.job.name
print(jobName)

RSGCore.Functions.GetCoords

  • This function operates very similarly to how the native GetEntityCoords does, but it returns the heading as well
---@param entity number
---@return vector4
function RSGCore.Functions.GetCoords(entity)
    local coords = GetEntityCoords(entity, false)
    local heading = GetEntityHeading(entity)
    return vector4(coords.x, coords.y, coords.z, heading)
end

-- Example

local coords = RSGCore.Functions.GetCoords(PlayerPedId())
print(coords)

RSGCore.Functions.HasItem

  • Returns whether a player has a certain item
function RSGCore.Functions.HasItem(items, amount)
    return exports['rsg-inventory']:HasItem(items, amount)
end

local hasItem = RSGCore.Functions.HasItem('my_cool_item', 1)
print(hasItem)

RSGCore.Functions.TriggerCaback

  • Function used to cal from the client to the server anbd receive a value back
function RSGCore.Functions.TriggerCallback(name, cb, ...)
    RSGCore.ServerCallbacks[name] = cb
    TriggerServerEvent('RSGCore:Server:TriggerCallback', name, ...)
end

-- Example

RSGCore.Functions.TriggerCallback('callbackName', function(result)
    print('I got this from the CreateCallBack -->  '..result)
end, 'my_parameter_name')

RSGCore.Functions.GetVehicles

  • Returns a vehice game pool (for backwards compatibility) - Not worth using
function RSGCore.Functions.GetVehicles()
    return GetGamePool('CVehicle')
end

-- Example

local vehicles = RSGCore.Functions.GetVehicles()
print(RSGCore.Debug(vehicles))

OR -- preferred method

local vehicles = GetGamePool('CVehicle')
print(RSGCore.Debug(vehicles))

RSGCore.Function.GetCoreObject

  • Returns the core object of the game
exports('GetCoreObject', function()
    return RSGCore
end)

local RSGCore = exports['rsg-core']:GetCoreObject()

OR -- call the core in a single file that loads before the others

RSGCore = exports['rsg-core']:GetCoreObject()

RSGCore.Function.GetPlayers

  • Returns a table of all players in the game - not worth using
function RSGCore.Functions.GetPlayers()
    return GetActivePlayers()
end

-- Example

local players = RSGCore.Functions.GetPlayers()
print(RSGCore.Debug(players))

OR -- preferred method

local players = GetActivePlayers()
print(RSGCore.Debug(players))

RSGCore.Function.GetPeds

  • Returns a model hash filtered ped game pool
function RSGCore.Functions.GetPeds(ignoreList)
    local pedPool = GetGamePool('CPed')
    local peds = {}
    local ignoreTable = {}
    ignoreList = ignoreList or {}
    for i = 1, #ignoreList do
        ignoreTable[ignoreList[i]] = true
    end
    for i = 1, #pedPool do
        if not ignoreTable[pedPool[i]] then
            peds[#peds + 1] = pedPool[i]
        end
    end
    return peds
end

-- Example

local peds = RSGCore.Functions.GetPeds({`mp_m_freemode_01`})
print(RSGCore.Debug(peds))

RSGCore.Function.GetClosestPed

  • Returns the closest ped to the player’s position
function RSGCore.Functions.GetClosestPed(coords, ignoreList)
    local ped = PlayerPedId()
    if coords then
        coords = type(coords) == 'table' and vec3(coords.x, coords.y, coords.z) or coords
    else
        coords = GetEntityCoords(ped)
    end
    ignoreList = ignoreList or {}
    local peds = RSGCore.Functions.GetPeds(ignoreList)
    local closestDistance = -1
    local closestPed = -1
    for i = 1, #peds, 1 do
        local pedCoords = GetEntityCoords(peds[i])
        local distance = #(pedCoords - coords)

        if closestDistance == -1 or closestDistance > distance then
            closestPed = peds[i]
            closestDistance = distance
        end
    end
    return closestPed, closestDistance
end

-- Example

local coords = GetEntityCoords(PlayerPedId())
local closestPed, distance = RSGCore.Functions.GetClosestPed(coords)
print(closestPed, distance)

RSGCore.Functions.GetClosestVehicle

  • Returns the closest vehicle to the player
function RSGCore.Functions.GetClosestVehicle(coords)
    local ped = PlayerPedId()
    local vehicles = GetGamePool('CVehicle')
    local closestDistance = -1
    local closestVehicle = -1
    if coords then
        coords = type(coords) == 'table' and vec3(coords.x, coords.y, coords.z) or coords
    else
        coords = GetEntityCoords(ped)
    end
    for i = 1, #vehicles, 1 do
        local vehicleCoords = GetEntityCoords(vehicles[i])
        local distance = #(vehicleCoords - coords)

        if closestDistance == -1 or closestDistance > distance then
            closestVehicle = vehicles[i]
            closestDistance = distance
        end
    end
    return closestVehicle, closestDistance
end

-- Example

local coords = GetEntityCoords(PlayerPedId())
local closestVehicle, distance = RSGCore.Functions.GetClosestVehicle(coords)
print(closestVehicle, distance)

RSGCore.Functions.GetClosestObject

  • Returns the closest player to the client
function RSGCore.Functions.GetClosestObject(coords)
    local ped = PlayerPedId()
    local objects = GetGamePool('CObject')
    local closestDistance = -1
    local closestObject = -1
    if coords then
        coords = type(coords) == 'table' and vec3(coords.x, coords.y, coords.z) or coords
    else
        coords = GetEntityCoords(ped)
    end
    for i = 1, #objects, 1 do
        local objectCoords = GetEntityCoords(objects[i])
        local distance = #(objectCoords - coords)
        if closestDistance == -1 or closestDistance > distance then
            closestObject = objects[i]
            closestDistance = distance
        end
    end
    return closestObject, closestDistance
end

-- Example

local coords = GetEntityCoords(PlayerPedId())
local closestObject, distance = RSGCore.Functions.GetClosestObject(coords)
print(closestObject, distance)

RSGCore.Functions.GetPlayersFromCoords

  • Returns a list of players within a certain radius from the given coordinates
function RSGCore.Functions.GetPlayersFromCoords(coords, distance)
    local players = GetActivePlayers()
    local ped = PlayerPedId()
    if coords then
        coords = type(coords) == 'table' and vec3(coords.x, coords.y, coords.z) or coords
    else
        coords = GetEntityCoords(ped)
    end
    distance = distance or 5
    local closePlayers = {}
    for _, player in ipairs(players) do
        local targetCoords = GetEntityCoords(GetPlayerPed(player))
        local targetdistance = #(targetCoords - coords)
        if targetdistance <= distance then
            closePlayers[#closePlayers + 1] = player
        end
    end
    return closePlayers
end

-- Example

local coords = GetEntityCoords(PlayerPedId())
local radius = 5.0
local closestPlayers = RSGCore.Functions.GetPlayersFromCoords(coords, radius)
print(RSGCore.Debug(closestPlayers))

RSGCore.Functions.SpawnVehicle

  • Spawns a vehicle at the given coordinates with the given model and color
---@param model string|number
---@param cb? fun(vehicle: number)
---@param coords? vector4 player position if not specified
---@param isnetworked? boolean defaults to true
---@param teleportInto boolean teleport player to driver seat if true
function RSGCore.Functions.SpawnVehicle(model, cb, coords, isnetworked, teleportInto)
    local playerCoords = GetEntityCoords(cache.ped)
    local combinedCoords = vec4(playerCoords.x, playerCoords.y, playerCoords.z, GetEntityHeading(cache.ped))
    coords = type(coords) == 'table' and vec4(coords.x, coords.y, coords.z, coords.w or combinedCoords.w) or coords or combinedCoords
    model = type(model) == 'string' and joaat(model) or model
    if not IsModelInCdimage(model) then return end

    isnetworked = isnetworked == nil or isnetworked
    lib.requestModel(model)
    local veh = CreateVehicle(model, coords.x, coords.y, coords.z, coords.w, isnetworked, false)
    local netid = NetworkGetNetworkIdFromEntity(veh)
    SetVehicleHasBeenOwnedByPlayer(veh, true)
    SetNetworkIdCanMigrate(netid, true)
    SetModelAsNoLongerNeeded(model)
    if teleportInto then TaskWarpPedIntoVehicle(cache.ped, veh, -1) end
    if cb then cb(veh) end
end

--- Example

local coords = RSGCore.Functions.GetCoords(PlayerPedId()) -- Ensure `coords` is vector4 with a heading value
coords = vec4(coords.x, coords.y, coords.z, coords.w or GetEntityHeading(PlayerPedId()))

RSGCore.Functions.SpawnVehicle('CART01', function(veh)
    SetEntityHeading(veh, coords.w) -- Set the heading of the vehicle
    TaskWarpPedIntoVehicle(PlayerPedId(), veh, -1) -- Teleport player into the vehicle
end, coords, true)

RSGCore.Functions.DeleteVehicle

  • Deletes a vehicle at the given entity ID
function RSGCore.Functions.DeleteVehicle(vehicle)
    SetEntityAsMissionEntity(vehicle, true, true)
    DeleteVehicle(vehicle)
end

-- Example

local ped = PlayerPedId()
local veh = GetVehiclePedIsUsing(ped)
if veh ~= 0 then
    RSGCore.Functions.DeleteVehicle(veh)
else
    local pcoords = GetEntityCoords(ped)
    local vehicles = GetGamePool('CVehicle')
    for k, v in pairs(vehicles) do
        if #(pcoords - GetEntityCoords(v)) <= 5.0 then
            RSGCore.Functions.DeleteVehicle(v)
        end
    end
end

OR -- preferred method

local ped = PlayerPedId()
local veh = GetVehiclePedIsUsing(ped)
if veh ~= 0 then
    SetEntityAsMissionEntity(veh, true, true)
    DeleteVehicle(veh)
else
    local pcoords = GetEntityCoords(ped)
    local vehicles = GetGamePool('CVehicle')
    for k, v in pairs(vehicles) do
        if #(pcoords - GetEntityCoords(v)) <= 5.0 then
            SetEntityAsMissionEntity(v, true, true)
            DeleteVehicle(v)
        end
    end
end