Help with my GetDistanceBetweenCoords

Hello, i’m trying to have a script that trigger a function when a player is near certain coordinates, but the i’m stuck rightnow because the script is in error

SCRIPT ERROR: @tutocoordnotif/tutocoordnotif.lua:24: attempt to call a nil value (global ‘GetDistanceBetweenCoords’)

i think it’s because i try to get the playerCoords when the player isn’t actually loaded, also i don’t think that the never ending while loop is a good practice, maybe someone can me on this one ?

Here’s my script code at the moment.

server-side

local QBCore = exports['qb-core']:GetCoreObject()

-- vector3(903.18, 14.19, 79.03)

-- Define the radius around the coordinates where the event will trigger
local triggerRadius = 5.0

-- Create a function to check if a player is within the specified coordinates
function IsPlayerInCoords(playerId)
    local player = playerId
    local ped = GetPlayerPed(player)
    local playerCoords = GetEntityCoords(ped)
    print(playerCoords)

    -- Define the coordinates of the area where you want to trigger an event
    local triggerCoords = {
        x = 903.18,
        y = 14.19,
        z = 79.03
    }

    local triggerRadius = 5.0
    
    local distance = GetDistanceBetweenCoords(playerCoords.x, playerCoords.y, playerCoords.z, triggerCoords.x, triggerCoords.y, triggerCoords.z, true)
    
    return distance <= triggerRadius
end
    
-- Function to periodically check player coordinates
function CheckPlayerCoords()
    Citizen.CreateThread(function()
        while true do
            Citizen.Wait(500) -- Adjust the interval as needed (in milliseconds)
            for _, player in ipairs(GetPlayers()) do
                print(player)
                if IsPlayerInCoords(player) then
                    -- The player is in the specified coordinates; you can trigger your desired action here.
                    TriggerClientEvent('playerEnteredCoords', player)
                end
            end
        end
    end)
end

-- Register an event to start checking player coordinates
RegisterServerEvent('tutocoordnotif:startChecking')
AddEventHandler('tutocoordnotif:startChecking', function()
    CheckPlayerCoords()
end)

-- Start checking player coordinates when a player connects
AddEventHandler('playerJoining', function()
    TriggerEvent('tutocoordnotif:startChecking')
end)

client side

local QBCore = exports['qb-core']:GetCoreObject()

-- Player loading in after selecting/making a character
-- This event and state change is useful for triggering certain code to happen when a player loads in

local isLoggedIn = false -- Initialize a global variable in our file to keep track of player login status

RegisterNetEvent('QBCore:Client:OnPlayerLoaded', function() -- this event is called when a player loads in
    isLoggedIn = true
    print('You have loaded') -- prints to your F8 console
end)

-- Register a client event to handle the action when a player enters the coordinates
RegisterNetEvent('playerEnteredCoords')
AddEventHandler('playerEnteredCoords', function()
    print('You have loadedeeee')
    QBCore.Functions.Notify('This is a test', 'success', 5000)

end)

Thank you sincerely.

This is not the case.

As the error states, the value (or in this case, the function) is nil; another way of saying it is undefined. The reason it is undefined is because it is a client side native only.

Refer to the post below for a work around:

1 Like

Hello ChristopherM and thank you for you answer,

Is the never ending while loop a good way to do what i want to do ?

There’s not too many ways to achieve what you’re wanting to do without using a loop. What I would personally suggest is actually having two loops one inside the other, the top level loop only running every 3 seconds or so and checking if the player is within say 100 m of the position, that way it’s not checking every 500 milliseconds even if they’re on the complete opposite side of the map; then your secondary interior loop can run every 500 milliseconds.

1 Like

This topic was automatically closed 2 days after the last reply. New replies are no longer allowed.