TGRHavoc / live_map

A FiveM addon for live maps
https://docs.tgrhavoc.co.uk/livemap-resource/
62 stars 38 forks source link

Question (not urgent, also sorry for all the questions recently) #56

Closed WolfoTheWolf closed 3 years ago

WolfoTheWolf commented 3 years ago

Hey there! For anyone who knows how I could fix this its not a major issue as of now but if someone has a solution it would be appreciated.

HOW I HAVE IT SET UP ====

So once a player leaves there car it removes them from the script, once they get back in a police car that is under the EMERGENCY category it re-adds them to the map

ISSUE == Issue is that once they exit the police car then get back in it says there are 2 of the same people in the group (will go away after a map refresh via the browser

1 2 3

TGRHavoc commented 3 years ago

Would it be possible to send me your whole client.lua file? Either on here or through pastebin?

It should be able to handle someone getting in and out of vehicles without breaking...

WolfoTheWolf commented 3 years ago

Yup here you go

            LiveMap - A LiveMap for FiveM servers
              Copyright (C) 2017  Jordan Dalton

This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.

This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
GNU General Public License for more details.

You should have received a copy of the GNU General Public License
along with this program in the file "LICENSE".  If not, see <http://www.gnu.org/licenses/>.
]]

--[[
    This file is an example. You can create your own resource that does something
        similar but, updates diifferent values (if you want).
    This is literally to show you how to use the thing
]]
-- This is the data that is gooing to be sent over websockets
local defaultDataSet = {
    ["pos"] = { x=0, y=0, z=0 }, -- Position of the player (vector(x, y, z))
    ["Vehicle"] = "none", -- Vehicle player is in (if any)
  --  ["Weapon"] = "Unarmed", -- Weapon player has equiped (if any)  REMOVED, NOT SHOWING WEAPONS
    ["icon"] = 6, -- Player blip id (will change with vehicles)
    ["Licence Plate"] = nil, -- To showcase the removal off data :D
    ["Location"] = "State of San Andreas", -- Player location
}

local temp = {}

-- Table to keep track of the updated data
local beenUpdated =  {}

function updateData(name, value)
    table.insert(beenUpdated, name)
    defaultDataSet[name] = value
end

function doVehicleUpdate()
    local vehicle = GetVehiclePedIsIn(PlayerPedId(), 0)

    if temp["vehicle"] ~= vehicle and vehicle ~= 0 then
        --NEW 
       if ( GetVehicleClass( vehicle ) == 18 ) then ]

        TriggerServerEvent("livemap:playerSpawned")  

       for key,val in pairs(defaultDataSet) do 
           TriggerServerEvent("livemap:AddPlayerData", key, val) 
       end -- end of new

        local vehNameHash = GetHashKey(GetDisplayNameFromVehicleModel(GetEntityModel(vehicle)))
        local reversedVehicleName = exports[GetCurrentResourceName()]:reverseVehicleHash(vehNameHash)
        updateData("Vehicle", reversedVehicleName)
        temp["vehicle"] = vehicle

    end

end

    local plate = GetVehicleNumberPlateText(vehicle)

    plate = plate:gsub("^%s*(.-)%s*$", "%1") -- Remove whitespace at the start and end of the plate (for plate check)

    if plate == "FIVE M" then
        plate = plate .. " (Spawned In)"
    end

    if defaultDataSet["Licence Plate"] ~= plate then
        --Citizen.Trace("Updating plate: " .. plate)
        updateData("Licence Plate", plate)
    end
end

function doIconUpdate()
    local ped = PlayerPedId()
    local newSprite = 6 -- Default to the player one

    if IsEntityDead(ped) then
        newSprite = 163 -- Using GtaOPassive since I don't have a "death" icon :(
    else
        if IsPedSittingInAnyVehicle(ped) then

            -- Change icon to vehicle
            -- our temp table should still have the latest vehicle
            local vehicle = temp["vehicle"]
            local vehicleModel = GetEntityModel(vehicle)
            local h = GetHashKey

         --REMOVED CAR MODELS ONLY POLICE IS LEFT
            if (vehicleModel == h("policeold2") or vehicleModel == h("policeold1") or vehicleModel == h("policet") or vehicleModel == h("police") or vehicleModel == h("police2") or vehicleModel == h("police3") or vehicleModel == h("policeb") or vehicleModel == h("riot") or vehicleModel == h("sheriff") or vehicleModel == h("sheriff2") or vehicleModel == h("pranger")) then
                newSprite = 56 -- PoliceCar

            end
        end
    end

    if defaultDataSet["icon"] ~= newSprite then
        updateData("icon", newSprite)
    end
end

local firstSpawn = true

--[[
    When the player spawns, make sure we set their ID in the data that is going
        to be sent via sockets.
]]
AddEventHandler("playerSpawned", function(spawn)
    if firstSpawn then
        TriggerServerEvent("livemap:playerSpawned") -- Set's the ID in "playerData" so it will get send va sockets

        -- Now send the default data set
        for key,val in pairs(defaultDataSet) do
            TriggerServerEvent("livemap:AddPlayerData", key, val)
        end

        firstSpawn = false
    end
end)

Citizen.CreateThread(function()
    while true do
        Wait(10)

        if NetworkIsPlayerActive(PlayerId()) then

            -- Update position, if it has changed
            local x,y,z = table.unpack(GetEntityCoords(PlayerPedId()))
            local x1,y1,z1 = defaultDataSet["pos"].x, defaultDataSet["pos"].y, defaultDataSet["pos"].z

            local dist = Vdist(x, y, z, x1, y1, z1)

            if (dist >= 5) then
                -- Update every 5 meters.. Let's reduce the amount of spam
                -- TODO: Maybe make this into a convar (e.g. accuracy_distance)
                updateData("pos", {x = x, y=y, z=z})

                -- Reverse the street, area and zone of the current location
                local streetname = exports[GetCurrentResourceName()]:reverseStreetHash(GetStreetNameAtCoord(x,y,z))
                local zone = exports[GetCurrentResourceName()]:reverseZoneHash(GetHashKey(GetNameOfZone(x, y, z)))
                local area = exports[GetCurrentResourceName()]:reverseAreaHash(GetHashOfMapAreaAtCoords(x, y, z))

                if (temp["streetname"] ~= streetname) or (temp["zone"] ~= zone) or (temp["area"] ~= area) then
                    local locationString = string.format("%s, %s (%s)", streetname, zone, area)

                    updateData("Location", locationString)
                    temp["streetname"] = streetname
                    temp["zone"] = zone
                    temp["area"] = area
                end

            end

            -- Update weapons

            -- Update Vehicle (and icon)
            if IsPedInAnyVehicle(PlayerPedId()) then
                doVehicleUpdate()

            elseif defaultDataSet["Licence Plate"] ~= nil or defaultDataSet["Vehicle"] ~= nil then
                -- No longer in a vehicle, remove "Licence Plate" if present
                defaultDataSet["Licence Plate"] = nil
                defaultDataSet["Vehicle"] = nil
                temp["vehicle"] = nil
                -- Remove it from socket communication
                TriggerServerEvent("livemap:RemovePlayerData", "Licence Plate")
                TriggerServerEvent("livemap:RemovePlayerData", "Vehicle")
                TriggerServerEvent("livemap:RemovePlayer") -- REMOVES PLAYER

            end

            doIconUpdate()

            -- Make sure the updated data is up-to-date on socket server as well
            for i,k in pairs(beenUpdated) do
                --Citizen.Trace("Updating " .. k)
                TriggerServerEvent("livemap:UpdatePlayerData", k, defaultDataSet[k])

                table.remove(beenUpdated, i)

            end

        end

    end

end)
TGRHavoc commented 3 years ago

Not tested it but, something like the following should be more what you're looking for. If you could test and see if the issue of multiple people showing fixes itself I would appreciate it. It it still persists then, I'll have to set up a server and dive into why :)

-- This is the data that is gooing to be sent over websockets
local defaultDataSet = {
    ["pos"] = { x=0, y=0, z=0 }, -- Position of the player (vector(x, y, z))
    ["Vehicle"] = "none", -- Vehicle player is in (if any)
    ["icon"] = 6, -- Player blip id (will change with vehicles)
    ["Licence Plate"] = nil, -- To showcase the removal off data :D
    ["Location"] = "State of San Andreas", -- Player location
}

local temp = {}

-- Table to keep track of the updated data
local beenUpdated =  {}

function updateData(name, value)
    table.insert(beenUpdated, name)
    defaultDataSet[name] = value
end

function doVehicleUpdate()
    local vehicle = GetVehiclePedIsIn(PlayerPedId(), 0)

    if temp["vehicle"] ~= vehicle and vehicle ~= 0 then
        -- Update it
        local vehicleClass = GetVehicleClass(vehicle)

        -- Added reverseWeaponHash to the vehicle display name to convert the vehicle name to a nicer version.
        -- To change, modify the "reverse_car_hashes.lua" file
        local vehNameHash = GetHashKey(GetDisplayNameFromVehicleModel(GetEntityModel(vehicle)))
        local reversedVehicleName = exports[GetCurrentResourceName()]:reverseVehicleHash(vehNameHash)
        updateData("Vehicle", reversedVehicleName)
        temp["vehicle"] = vehicle
    end

    local plate = GetVehicleNumberPlateText(vehicle)
    plate = plate:gsub("^%s*(.-)%s*$", "%1") -- Remove whitespace at the start and end of the plate (for plate check)

    if plate == "FIVE M" then
        plate = plate .. " (Spawned In)"
    end

    if defaultDataSet["Licence Plate"] ~= plate then
        --Citizen.Trace("Updating plate: " .. plate)
        updateData("Licence Plate", plate)
    end
end

function doIconUpdate()
    local ped = PlayerPedId()
    local newSprite = 6 -- Default to the player one

    if IsEntityDead(ped) then
        newSprite = 163 -- Using GtaOPassive since I don't have a "death" icon :(
    else
        if IsPedSittingInAnyVehicle(ped) then
            -- Change icon to vehicle
            -- our temp table should still have the latest vehicle
            local vehicle = temp["vehicle"]
            local vehicleModel = GetEntityModel(vehicle)
            local h = GetHashKey

            if (vehicleModel == h("policeold2") or vehicleModel == h("policeold1") or vehicleModel == h("policet") or vehicleModel == h("police") or vehicleModel == h("police2") or vehicleModel == h("police3") or vehicleModel == h("policeb") or vehicleModel == h("riot") or vehicleModel == h("sheriff") or vehicleModel == h("sheriff2") or vehicleModel == h("pranger")) then
                newSprite = 56 -- PoliceCar
            else
                newSprite = 225 -- PersonalVehicleCar
            end
        end
    end

    if defaultDataSet["icon"] ~= newSprite then
        updateData("icon", newSprite)
    end
end

local firstSpawn = true

--[[
    When the player spawns, make sure we set their ID in the data that is going
        to be sent via sockets.
]]
AddEventHandler("playerSpawned", function(spawn)
    if firstSpawn then
        TriggerServerEvent("livemap:playerSpawned") -- Set's the ID in "playerData" so it will get send va sockets

        -- Now send the default data set
        --[[ We don't want to show the player UNLESS they're in a police vehicle. So, no need for this anymore
            for key,val in pairs(defaultDataSet) do
            TriggerServerEvent("livemap:AddPlayerData", key, val)
        end
        ]]

        firstSpawn = false
    end
end)

Citizen.CreateThread(function()
    while true do
        Wait(10)

        if NetworkIsPlayerActive(PlayerId()) then
            -- Do the police checks **FIRST**. That way, data will only be sent if they're in an emergency vehicle.

            -- Update Vehicle (and icon)
            if IsPedInAnyVehicle(PlayerPedId()) then
                doVehicleUpdate()
                doIconUpdate()

                -- We now want to update their position.
                if (defaultDataSet["icon"] ~= 56) then --They're not in a emergency vehicle
                    break -- Exit the if statement above. 
                end

                -- Update position, if it has changed
                local x,y,z = table.unpack(GetEntityCoords(PlayerPedId()))
                local x1,y1,z1 = defaultDataSet["pos"].x, defaultDataSet["pos"].y, defaultDataSet["pos"].z

                local dist = Vdist(x, y, z, x1, y1, z1)

                if (dist >= 5) then
                    -- Update every 5 meters.. Let's reduce the amount of spam
                    -- TODO: Maybe make this into a convar (e.g. accuracy_distance)
                    updateData("pos", {x = x, y=y, z=z})

                    -- Reverse the street, area and zone of the current location
                    local streetname = exports[GetCurrentResourceName()]:reverseStreetHash(GetStreetNameAtCoord(x,y,z))
                    local zone = exports[GetCurrentResourceName()]:reverseZoneHash(GetHashKey(GetNameOfZone(x, y, z)))
                    local area = exports[GetCurrentResourceName()]:reverseAreaHash(GetHashOfMapAreaAtCoords(x, y, z))

                    if (temp["streetname"] ~= streetname) or (temp["zone"] ~= zone) or (temp["area"] ~= area) then
                        local locationString = string.format("%s, %s (%s)", streetname, zone, area)

                        updateData("Location", locationString)
                        temp["streetname"] = streetname
                        temp["zone"] = zone
                        temp["area"] = area
                    end
                end

            elseif defaultDataSet["Licence Plate"] ~= nil or defaultDataSet["Vehicle"] ~= nil then
                temp["vehicle"] = nil

                TriggerServerEvent("livemap:RemovePlayer")
            end

            -- Make sure the updated data is up-to-date on socket server as well
            for i,k in pairs(beenUpdated) do
                --Citizen.Trace("Updating " .. k)
                TriggerServerEvent("livemap:UpdatePlayerData", k, defaultDataSet[k])
                table.remove(beenUpdated, i)
            end

        end

    end
end)
WolfoTheWolf commented 3 years ago

Does not seem to be working, also this is the only error Capture

WolfoTheWolf commented 3 years ago

So, I haft to restart the script via the server. _ If I get in the police car it does not update, when I restart the script while in the police car it shows on the map, when I get out I do not disappear it just keeps the car icon and all the info on the map, if I restart the script again it then removes me from the map if I am not in the car

TGRHavoc commented 3 years ago

In all honesty, it looks like it's the resource client that's causing these issues. Doesn't seem to be sending the position to the interface. Probably a logic error somewhere... I can probably debug the client code but, I've got over stuff to do so it might take a while to do and respond.

WolfoTheWolf commented 3 years ago

Actually I just seen the error in the f8 console. But yea take your time no rush Capture