GetGroundZFor_3Coord not working as intended?

Hello there,

I was working on making a client script that would teleport a player/vehicle to a waypoint.
I have done some coding for a private mod menu for GTA V and implemented my code from there but was unable to get a result that is pleasing.

With the below code I end up below the map no matter what:

function ShowNotification( text )
    SetNotificationTextEntry("STRING")
    AddTextComponentSubstringPlayerName(text)
    DrawNotification(false, false)
end

RegisterCommand('wp', function(source, args, rawCommand)
    local blip = GetFirstBlipInfoId(8)

    if not DoesBlipExist(blip) then
        ShowNotification("No waypoint is set!")

        return
    end

    local x, y, z = table.unpack(GetBlipCoords(blip))
    local targetEntity = PlayerPedId()

    if IsPedInAnyVehicle(targetEntity) then
        targetEntity = GetVehiclePedIsIn(targetEntity, false)
    end

    Citizen.CreateThread(function()
        for i = 0, 32, 1 do
            if i ~= 0 then
                for attempt = 1000, 0, -100 do
                    RequestCollisionAtCoord(x, y, attempt)

                    Citizen.Wait(0)
                end
            end

            local groundFound, groundZ = GetGroundZFor_3dCoord(x, y, attempt, true)
            if groundFound then
                z = groundZ

                break
            end
        end

        SetEntityCoords(targetEntity, x, y, z + 2, false, false, false, true)
    end)
end, false)

I have checked out the following topics without any result:

I advise you to use this which works very well. https://github.com/qalle-fivem/esx_marker

Thank you for your response.

I made the script with the intention to run it without ESX or VRP.

Incorrect solution

I have modified my existing script and the following works really well.

function ShowNotification( text )
    SetNotificationTextEntry("STRING")
    AddTextComponentSubstringPlayerName(text)
    DrawNotification(false, false)
end

RegisterCommand('wp', function(source, args, rawCommand)
    local blip = GetFirstBlipInfoId(8)

    if not DoesBlipExist(blip) then
        ShowNotification("No waypoint is set!")

        return
    end

    local x, y, z = table.unpack(GetBlipCoords(blip))

    for i=1, 1000 do
        SetPedCoordsKeepVehicle(PlayerPedId(), x, y, i + 0.0)

        local groundFound, groundZ = GetGroundZFor_3dCoord(x, y, i + 0.0)
        if groundFound then
            z = height + 1.0

            break
        end

        Citizen.Wait(5)
    end

    SetPedCoordsKeepVehicle(PlayerPedId(), x, y, z)
end, false)
1 Like

Another post was recently responded too about the same thing, by dbubs, found here: Can't reliably get the Ground Z position - #4 by d-bubble

What a way to revive an old thread, I don’t use this method anymore.

You can assume old posts of mine are trash.