[SOLVED] Player's location won't send to server

Hello thank you for taking the time to try and help me. I have a script that I am creating that needs the player street and zone on the server side. This is my current code.

client.lua


Citizen.CreateThread(function()
local zone = zones[GetNameOfZone(table.unpack(GetEntityCoords(PlayerPedId(),true)))]
local street = GetStreetNameFromHashKey(GetStreetNameAtCoord(table.unpack(GetEntityCoords(PlayerPedId(),true))))
    while true do
        TriggerServerEvent("sendpos", street, zone)
        Citizen.Wait(100)
    end
end)

server.lua


local playerstreet = {}
local playerzone = {}
RegisterServerEvent("sendpos")
AddEventHandler("sendpos", function(street, zone)
  if zone ~= nil then
    playerzone[source] = zone
  else
    playerzone[source] = "N/A"
  end
  if street ~= nil then
    playerstreet[source] = street
  else
    playerstreet[source] = "N/A"
  end
end)

Whenever I request the variable in lets say a chat message

RegisterCommand('location', function(source, args, RawCommand)
    TriggerClientEvent('chatMessage', source, "[SYSTEM]", {255, 0, 0}, "Your location is " .. playerstreet[source] .. ", " .. playerzone[source] .. ""
end)

It will return , West Vinewood NO MATTER what I do… UNLESS I restart the script while the server is running and then it works fine! If anyone has any ideas please get back with me!

1 Like

Your variables are not inside the loop so they are not being updated. Move them inside the while true do loop and increase the wait time (you do not want to ping the server every 100ms)

1 Like

So this

Citizen.CreateThread(function()
    while true do
        local zone = zones[GetNameOfZone(table.unpack(GetEntityCoords(PlayerPedId(),true)))]
        local street = GetStreetNameFromHashKey(GetStreetNameAtCoord(table.unpack(GetEntityCoords(PlayerPedId(),true))))
        TriggerServerEvent("sendpos", street, zone)
        Citizen.Wait(5000)
    end
end)
2 Likes

Yes. Do that.

Thank you for your help, I kinda feel stupid… It works perfectly now!

Welcome. (happens to everyone :wink:)