[Help] Issue with Entities found when Raycasting

Hello, I initially tried to resolve this problem by using the Invoke Native method to use GetPedsNearbyPed however that native is not available at the moment. I decided to try a raycasting solution which was suggested by a few folks on Discord, and came up with the following:

Citizen.CreateThread(function()

    pedstate = {{0,0,0}}
    objecttable = {}

    while true do
      Citizen.Wait(0)
      
        tempobject = 0
        Citizen.Trace("test before raycast") -- trace
        
        packedtable = RaycastForNearbyPeds(GetPlayerPed(-1))
        pedtable = table.unpack(packedtable)
        
        tablelength = GetTableLength(pedtable)
        Citizen.Trace("Table Length 1st check: " .. tablelength)
        Citizen.Trace("test after raycast") -- trace
        for i = 1, (tablelength + 1)  do 
            Citizen.Trace("Inside the loop") -- trace
            currentped = pedtable[i]
            currentstate = IsPedDeadOrDying(currentped, 1)
           
            temptable = {currentped, currentstate, laststate}
            table.insert(pedstate, temptable)
            Citizen.Trace("i = " .. i) -- trace
            laststate = pedstate[i][2]
            
            Citizen.Trace("Table is currently this big: " .. tablelength)

            if pedstate[i][2] ~= pedstate[i][3] then
                Citizen.Trace("State Changed for" .. currentped) -- trace
                x, y, z = table.unpack(GetEntityCoords(currentped, true))
                x = x + math.random(-3,3)
                y = y + math.random(-3,3)
                z = z + .1
                
                tempobject = CreateObject(0x113FD533, x, y, z, true, true, true)
                table.insert(objecttable, tempobject)
                SetEntityDynamic(tempobject, true)
                Citizen.Trace("Should have Spawned Money") -- trace
            end            
        end
    end
end)

function GetTableLength(temptable)
    local count = 0
    for _ in pairs(temptable) do
        count = count+1
    end
    
    return count
end

function RaycastForNearbyPeds(inputped)
    i = 0
    x, y, z = table.unpack(GetEntityCoords(inputped, true))
    detectedpeds = {}
    
    while (i < 200) do
        i = i + 1
            
            local x2 = x + math.random() + math.random(-99,99)
            local y2 = y + math.random() + math.random(-99,99)
            local z2 = z + math.random() + math.random(-3,3)
            
            local rayhandle = CastRayPointToPoint(x, y, z, x2, y2, z2, 4, inputped, 0)
            local _, _, _, _, result = GetRaycastResult(rayhandle)
            
            table.insert(detectedpeds, result)
            Citizen.Trace('detectedped: ' .. result)
    end
    return table.pack(detectedpeds)
end

The issue stems from the fact that I apparently do so many calls that I reach the pool limit which, in my case, is 650. I’m not quite sure what’s wrong with my code that it’s causing this. I’ve noticed that line 30 seems to return true consistently and always initiates the block of text underneath. I’m not quite sure why.

If anyone can take a look, and perhaps help me determine what’s causing the problem or a solution, I’d appreciate it.

Thank you for your time.

Example of in-game glitches: https://www.dropbox.com/s/ttmnivzn9daj68d/2017-07-10_00-44-37.mp4?dl=0

Each tick you are doing 200 * the operation ? ^^
It’s too much. I experimented a polynomial wind number client side. And 20 calculs like you each tick was almost too much.
I recommend to decrease the “200” number to get the good value. And a good ratio between calculating time / result

Why you used random instead of scanning for a radius :thinking:

I think it take more operations

It doesn’t matter how much you slow down the raycasting it’ll generally always lead to reaching an unknown pool size limit (650 raycasts).

The client stores them in memory for way too much time unfortunately and I need to do a lot of them in order for it to actually detect peds. I found a much cleaner / cheaper solution that I’ll be posting about soon.

Edit I’m wrong. Solved. 1-2 raycasts a tick is fine. Don’t recommend using point based raycasts, try the shape ones.

You only can have the entities near you right ? Cause the other arn’t load ? Even the networked one ? no ?

Yes, that’s correct. It is theoretically possible to get the networked ID’s but my system does not provide that functionality. There actually aren’t many reasons why you would need that functionality since Peds are synced for the most part anyways.

I want to delete ped, veh, and other entities which arn’t knowed