Script not working until restarting when connected

Hey, I got the Problem (and cant solve it) that my script is working fine but when I reconnect nothing works until I restart the script. Can someone pls help me? Thank you for the help :slight_smile:

Fun fact: the Marker is getting drawn like everytime

Citizen.CreateThread(function()
while true do
playerCoords = GetEntityCoords(playerPed)
DrawMarker(27, Config.exitMarker, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 3.0, 3.0, 3.0, 124, 0, 206, 100, false, false, 2, true, false, false, false)
if Vdist(Config.exitMarker, playerCoords) < 2 then
showInfobar(Config.exitText)
if IsControlJustReleased(0, 38) then
TriggerServerEvent(‘dd_entrysystem:CheckPlayerWhitelistStatus’)
end
end
Citizen.Wait(0)
end
end)

Is there any error when you reconnect? Have you try putting some debug? If so can you provide use some debug.

And try PlayerPedId() insted of PlayerPed

I ve got playerpedid defined in a local but no errors

Edit: when I come home from work I will put some „print“ in the code for debug

Here is the debug stuff (I edited the script a bit):
image

EDIT: the ------STUFF------- thing is not working as well (its ciming with an command) when I restart the script it both works …

That is likely the cause of this behavior: something you do on load is changing the player ped (e.g. calling SetPlayerModel) and you’re not updating the ‘local’.

still the problemm

I’ve had the same problem with some of my scripts.
Instead of assigning PlayerPedId() to a variable outside of loop, do it inside it.

Not the best or the most optimized way of handling a playerPed but you gotta do what you gotta do.

But if you want to keep your precious optimization as good as you can you can always overwrite the variable with current playerPed on “playerSpawned” event. But I found out its not always the best way and it sometimes didn’t work for me.

local playerPed = PlayerPedId()
AddEventHandler("playerSpawned", function()  
    playerPed = PlayerPedId() --or GetPlayerPed(-1)
end)
CreateThread(function()
    --some stuff
    while true do
       -whole drawMarker thing
    end
end)

Can you explain it more ? :woozy_face: What do you mean and what is the event handler for? Thank you for helping

He define the pedid on player spawn, can you try using PlayerPedId() instead of PlayerPed, and printing Vdist(Config.exitMarker, playerCoords) instead of “Test1”, so we can see what’s happening, and when you are ingame try moving a little bit to see if Vdist is changing

And please try 2.0 instead of 2, its a float, and sometimes it do need the .0 after it

Thank you I will try it out tomorrow

Keep us up to date, even if its working

Just wondering something have you declare you’re playerPed variable outside of a Citizen.CreatheThread(function())? If so its the problem, cause you can’t declare a playerped if the ped is not connected

f8 is empty

EDIT: when I restart scripts position is showing up in f8

You may want to try my idea.
Here is a piece of code from my own server for dealing with whether or not you’ve spawned or smth.

local ESX = exports.es_extended:getSharedObject()

XYZ.RegisterSpawnFunction = function(cb,loaded)
    if loaded then
        CreateThread(function()
            while not ESX.IsPlayerLoaded() do
                Wait(10)
            end
            cb()
        end)
    elseif not loaded then
        CreateThread(function()
            while not XYZ.Spawned and not ESX.IsPlayerLoaded() do
                Wait(10)
            end
            cb()
        end)
    end
end

XYZ.Spawned is the same thing as firstSpawned in some scripts, and ESX.IsPlayerLoaded() is because if you restart the script, you have firstSpawned set as false, and you can’t unload a player magically(unless you restart the whole framework, still its using ESX, standalone down below)

You can try doing something like this.

local playerPed = PlayerPedId()

AddEventHandler('playerSpawned', function() -- triggered with for example /revive for ESX
    playerPed = PlayerPedId() -- Override playerPed with current ped after respawn
end)

CreateThread(function()
    playerPed = PlayerPedId() -- just because you can :D
    while true do
        --Whole loop you have there
    end
end)

The second version is more elegant and better, standalone! No Framework required, but if you use ESX or QBCore you may want to check if player is loaded before the loop, for ESX it would be

local ESX = exports.es_extended:getSharedObject()
local playerPed = PlayerPedId()

AddEventHandler('playerSpawned', function() -- leave it for the sake of it
    playerPed = PlayerPedId()
end)

CreateThread(function()
    while not ESX.IsPlayerLoaded()do
        Wait(10)
    end
    playerPed = PlayerPedId() -- override after ped is really loaded
    while true do
        --Whole loop you have there
    end
end)

I left the event since you may loose the track of your current ped either way, it doesn’t eat much of optimization and it won’t hurt you. I personally would stick with the second example, its much better and more elegant. Unless you need the player to be specifically loaded before anything.

Anyway, I hope it helps!

Hey thank you verry much for helping me out that much, the thing is that i work with QB Core ^^

I’ve looked at source code of QBCore and found nothing like IsPlayerLoaded, you may be able to modify it a little to change the value to true of some variable.

But for your needs you can stick to the num 2 example. It should work just fine for you.
Lmk if it works or not :smiley:

P.S. when I say num 2 I mean the second example, not the last one

I use this trigger at the beginning, can I use it in this case as well?

image

still does not work… tried it your way and tried it my way … wtf is that

I am rewriting everything at the moment and I think i got the problem:

I did that:
image

but thats the right way:
image

I ll keep u up if it all works but going asleep now