[HELP] Get whether a bullet hits a marker or not

Hi there! I’m trying to figure out how to get when a bullet hits a marker. I’ve tried multiple ways but they don’t seem to work. So now I’m asking for help. I’ve provided the code down below. Thank’s for your help!

function startFlick360Game(gamemodeIndex, gamemode)
    local playerPed = PlayerPedId()
    FreezeEntityPosition(playerPed, true)
    SetEntityInvincible(playerPed, true)
    GiveWeaponToPed(playerPed, GetHashKey('WEAPON_HEAVYPISTOL'), 1000, false, true)
    SetCurrentPedWeapon(playerPed, GetHashKey('WEAPON_HEAVYPISTOL'), true)
    SetPedInfiniteAmmo(playerPed, true, GetHashKey('WEAPON_HEAVYPISTOL'))
    SetPedInfiniteAmmoClip(playerPed, true)
    toggleHudUIs(false)
    DisplayRadar(false)

    isGameActive = true
    gameStats = { score = 0, timeLeft = 60, shotsFired = 0, shotsHit = 0, accuracy = 0 }

    SendNUIMessage({
        action = 'openGamemodeStatsUI',
        score = gameStats.score,
        timeLeft = gameStats.timeLeft,
        accuracy = gameStats.accuracy
    })

    local targets = gamemode.targetLocations
    local currentTargetCoords = nil

    local function selectNewTarget()
        local newTarget = targets[math.random(#targets)]
        currentTargetCoords = vector3(newTarget.x, newTarget.y, newTarget.z + 0.5)
    end

    selectNewTarget()

    CreateThread(function()
        while isGameActive do
            if currentTargetCoords then
                DrawMarker(28, currentTargetCoords.x, currentTargetCoords.y, currentTargetCoords.z + 0.5, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.5, 0.5, 0.5, 255, 0, 0, 100, false, true, 2, false, nil, nil, false)
            end
            Wait(0)
        end
    end)

    CreateThread(function()
        local startTime = GetGameTimer()

        while isGameActive do
            local elapsed = math.floor((GetGameTimer() - startTime) / 1000)
            gameStats.timeLeft = math.max(0, 60 - elapsed)

            if IsPedShooting(playerPed) then
                gameStats.shotsFired = gameStats.shotsFired + 1

                local hit, impactCoords = GetPedLastWeaponImpactCoord(playerPed)
                if hit and currentTargetCoords then
                    local distance = #(impactCoords - currentTargetCoords)
                    if distance < 0.5 then
                        gameStats.score = gameStats.score + 400
                        gameStats.shotsHit = gameStats.shotsHit + 1
                        selectNewTarget()
                    else
                        gameStats.score = gameStats.score - 100
                    end
                end
            end

            gameStats.accuracy = gameStats.shotsFired > 0 and math.floor((gameStats.shotsHit / gameStats.shotsFired) * 100) or 0

            SendNUIMessage({
                action = 'updateGamemodeStatsUI',
                score = gameStats.score,
                timeLeft = gameStats.timeLeft,
                accuracy = gameStats.accuracy
            })

            if gameStats.score >= 20000 or gameStats.timeLeft <= 0 then
                resetGamemode(gamemodeIndex, gamemode)
                break
            end

            Citizen.Wait(0)
        end
    end)
end

First of all, Debug it. See where the bullet is hitting, what is the difference between the impact coords and the targetCoords, add prints ma guy! add prints to the whole process and see if it evens run properly.
My main guess here is that you are using a small 0.5 offset for the DrawMarker as well as when generating the new curentTargetCoords ( currentTargetCoords.z + 0.5 ). This difference is causing the issue inside the distance check ( if distance < 0.5 then ) , you are already adding a 0.5 offset to the targetCoords. so its logical that you are not meeting a restrict under 0.5 distance check because of it!
Also Instead of using DrawMarker to show the target coords , there are better ways. however its only visual improvement, so for now lets fix the logic itself!

Thank you for your reply. I’ve added so it prints where the bullet is impacted at, the target coords and the distance between them for better understanding on how far away the bullet acutally is. The problem is that I want to use markers as the target, but since they don’t have any collision the GetPedLastWeaponImpactCoord function will not work the way it should. Is there any prop that is similar to a green sphere? I know that there is a joga ball but I specifically want a green sphere. Or maybe there is another solution to it, I don’t know :person_shrugging:

Create an entity the size of your marker and set it invisible, do not disable collision and it will register hits, but it will register all hits so players walking into it etc and will affect actual physics occurring. As far as I am aware there is no way to detect bullet collisions on an entity with collisions disabled.
Also there’s this IsBulletInArea - FiveM Natives @ Cfx.re Docs but you’d run it every frame and check the location
Also this IsProjectileInArea - FiveM Natives @ Cfx.re Docs for non bullets I guess rockets Idrk

if your markers are not for example on a wall or something, the bullets will not have any collisions to hit to. so in turn the lastweaponimpact native will be useless
As JiminyKroket said, you need to spawn a dummy prop behind every marker and make it invisible. You can use SetEntityAlpha for making them invisible.
However this will in turn make some unexpected behaviors for the collisions, But this will only be for the client ped itself since the dummy props are client-sided, no need for them to be networked.
Another way of doing this will be predicting the bullet trajectory upon firing, This will involve more complicated math ,however there would not be a need for the dummy props ( Unless you decide to use raycast to detect the trajectory which in turn is not a good idea )
One of the last ways you can do this without all these workarounds, is getting the lastweaponimpactCoord and checking if the markercoords were within the trajectory of the bullet , lastweaponimpactcoord has hit something with a collision, However this will not work if there is nothing with collision behind the marker itself, for example if the marker was in the air and the player had to shoot in the air, this logic would fail.

Problem is fixed :grinning: