DrawMarker works only after restarting resource

So I made this script:
local markerPos = vector3(-263.10089111328,-967.615234375,31.224607467651)

Citizen.CreateThread(function()

local ped = GetPlayerPed(-1)

while true do 

Citizen.Wait(500)

    local playerCoords = GetEntityCoords(ped)

    local distance = #(playerCoords - markerPos)

if distance < 100.0 then

        DrawMarker(25, markerPos.x, markerPos.y, markerPos.z + 1, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 2.0, 2.0, 2.0, 255, 128, 0, 50, false, true, 2, nil, nil, false)

    else

        Citizen.Wait(2000)

    end

    local inMarker = false

if distance < 2.0 then

        inMarker = true

    end

if inMarker then

        TriggerServerEvent('vpj:adaugajob')

    end

end

end)
And when I start up the server, the resource starts up, no errors in console (neither client or server) but the marker isn’t drawn. If I restart the resource it works but if I leave the server and join back again it stops working and I have to restart it again.

Thats because the player doesnt exist yet when the resource is loaded when joining.

Try checking the player in a loop and if the player exists/isvalid then run the rest of the code

1 Like

Could you give me an example of how to do that?

Put the PlayerPedId() (or GetPlayerPed(-1)) call inside the loop.

By the way, your code will spam a server event a lot as you’re not doing any sort of check to trigger the event only once.

1 Like

It does work now, thanks for that.

But now how do I make it so it only triggers once? – I figured it out. Thanks for the help!