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.