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