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
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)
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â.
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)
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
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
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.
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
P.S. when I say num 2 I mean the second example, not the last one