Need help with loop

image
I am trying to make it so the text appears when the different of coordinates from the player and the ped is less than 3 and when greater than 3 it goes away.


here are my functions aswell

It doesn’t look like you’re updating playerCoords in that loop.
If you don’t update that, then the script will never know you moved.

Whatever you’re doing to obtain playerCoords, do it inside the loop.


this is the full code, it is finding my location between myself and the ped because when i start the script near the ped the text shows up but when i run away it still stays on my screen although when i start the script away from the ped the text just wont show up at all no matter how close I am, im thinking it has something to do with the loop as the if statement is not working everytime im nearby or far away

A bunch of problems and possible improvements with your code.

  1. As @Demonen pointed out, you should move the player = PlayerPedId() inside the Citizen.CreateThread()and playerCoords = GetEntityCoords(player) inside the while true do, so that the player’s coordinates are updated every tick. Also, it seems that the PlayerPedId() native doesn’t take any parameters, did you maybe mean to use GetPlayerPed(-1)? Although I think both have the same effect (I personally always used GetPlayerPed(-1) to get the local player).

  2. In the GetDistance() function, you’re passing what looks like 2 parameters of type vector3 then creating 2 new vector3s by unpacking the x, y, z fields… You can just do return #(originCoords - objectCoords) instead of that nonsense.

  3. You made your own function called RequestModel() which has the same name as a FiveM native and this may cause a conflict, may no be entirely correct (I’m like 80% sure), but a thing to keep in mind.

  4. You should try to use local variables more, if you don’t truly need global ones, such as your player and playerCoords variables.

  5. After you get it working as intended, you could also improve the performance of the while true do loop using a variable wait timer. For example, making the loop wait 1 or 2 seconds if the player is not in range and 0 or 1 miliseconds if the player is in range and you need to draw stuff every frame.

Ahh i see thanks for the help i get what ive done wrong now and ive taken your feedback on thanks for the response man i appreciate it

thanks for the help aswelll @Demonen

1 Like

No problem.