Lu-oxmysql Assistance

243 views Asked by At

I need help! this bug: [ script:oxmysql] Error: teleport-custom was unable to execute a query! [ script:oxmysql] Query: INSERT INTO player_locations (player_id, x, y, z) VALUES (?, ?, ?, ?) [ script:oxmysql] ["license:405702e0a3ae0630efaa0e0520c2ae1d40de74b6",null,1.4739839571120683e-8,0] [ script:oxmysql] Unknown column 'NaN' in 'field list'

and this is the code(server.lua):

-- Save player's location to the database
RegisterCommand('savelocation', function(source, args, rawCommand)
local x, y, z = table.unpack(GetEntityCoords(GetPlayerPed(-1)))
local playerId = GetPlayerIdentifier(source, 0)

    -- Check if x, y, or z are NaN (not a number) and replace them with 0
    if not x or not tonumber(x) then
        x = 0.0
    end
    if not y or not tonumber(y) then
        y = 0.0
    end
    if not z or not tonumber(z) then
        z = 0.0
    end
    
    local query = "INSERT INTO player_locations (player_id, x, y, z) VALUES (@player_id, @x, @y, @z)"
    local params = {
        ['@player_id'] = playerId,
        ['@x'] = x,
        ['@y'] = y,
        ['@z'] = z
    }
    
    exports.oxmysql:execute(query, params, function(affectedRows)
        if affectedRows > 0 then
            TriggerClientEvent('chatMessage', source, '^2Location saved!')
        else
            TriggerClientEvent('chatMessage', source, '^1Error saving location!')
        end
    end)

end, false)
\`
\-- Teleport the player to a random saved location
RegisterCommand('teleporttolocation', function(source, args, rawCommand)
local playerId = GetPlayerIdentifier(source, 0)

    local query = "SELECT * FROM player_locations WHERE player_id = @player_id ORDER BY RAND() LIMIT 1"
    local params = {
        ['@player_id'] = playerId
    }
    
    exports.oxmysql:fetch(query, params, function(result)
        if result and type(result) == 'table' and result[1] then
            local x, y, z = tonumber(result[1].x), tonumber(result[1].y), tonumber(result[1].z)
            if x and y and z then
                local playerPed = GetPlayerPed(-1)
                SetEntityCoords(playerPed, x, y, z, true, true, true)
                TriggerClientEvent('chatMessage', source, '^2Teleported to a saved location!')
            else
                TriggerClientEvent('chatMessage', source, '^1Invalid coordinates in the database!')
            end
        else
            TriggerClientEvent('chatMessage', source, '^1No saved locations found or database error!')
        end
    end)

end, false)

I tried to use multipole Ai software's(chatGPT, Barde...) but that didn't help what is the problem and how to fix it? I will appreciate your assistance

I couldnt fix it, I will appreciate your assistance

1

There are 1 answers

0
Comet1903 On

What you're trying to do is to save the location of the FiveM Server.

-- This means that you want to get the ped of the current user.
-- This only works in the FiveM client, not the server.
local ped = GetPlayerPed(-1)

On the server side, you can get the player's location by passing the player's ID in the GetPlayerPed(param1) function (Fivem Docs).

When registering a command (as you did), you'll get the player's ID as the first parameter (0 is the server, and when the ID is > 0, it's a player).

Try this instead:

-- Save player's location to the database
RegisterCommand('savelocation', function(source, args, rawCommand)
    local x, y, z = table.unpack(GetEntityCoords(GetPlayerPed(source)))
    local playerId = GetPlayerIdentifier(source, 0)

    -- Check if x, y, or z are NaN (not a number) and replace them with 0
    if not x or not tonumber(x) then
        x = 0.0
    end
    if not y or not tonumber(y) then
        y = 0.0
    end
    if not z or not tonumber(z) then
        z = 0.0
    end
    local query = "INSERT INTO player_locations (player_id, x, y, z) VALUES (@player_id, @x, @y, @z)"
    local params = {
        ['@player_id'] = playerId,
        ['@x'] = x,
        ['@y'] = y,
        ['@z'] = z
    }
    exports.oxmysql:execute(query, params, function(affectedRows)
        if affectedRows > 0 then
            TriggerClientEvent('chatMessage', source, '^2Location saved!')
        else
            TriggerClientEvent('chatMessage', source, '^1Error saving location!')
        end
    end)
end, false)