Improve cleanup of distance based peds

When changing character, this *should* remove the zone to allow the script to make a new one when switching
This commit is contained in:
Jim Shield
2025-08-05 12:34:25 +01:00
parent 6cfb776eb4
commit 2acd506504

View File

@@ -1,5 +1,6 @@
--- A table to keep track of all created Peds. --- A table to keep track of all created Peds.
local Peds = {} local Peds = {}
local distPeds = {}
--- Creates a distance-based Ped (pedestrian) that spawns when the player enters a specified area. --- Creates a distance-based Ped (pedestrian) that spawns when the player enters a specified area.
-- --
@@ -21,7 +22,7 @@ local Peds = {}
function makeDistPed(data, coords, freeze, collision, scenario, anim, synced, func) function makeDistPed(data, coords, freeze, collision, scenario, anim, synced, func)
local zoneCoords = type(data) == "table" and data.coords or coords local zoneCoords = type(data) == "table" and data.coords or coords
local randName = keyGen()..keyGen() local randName = keyGen()..keyGen()
createCirclePoly({ distPeds[#distPeds+1] = createCirclePoly({
name = randName, name = randName,
coords = vec3(zoneCoords.x, zoneCoords.y, zoneCoords.z - 1.03), coords = vec3(zoneCoords.x, zoneCoords.y, zoneCoords.z - 1.03),
radius = 50.0, radius = 50.0,
@@ -31,6 +32,7 @@ function makeDistPed(data, coords, freeze, collision, scenario, anim, synced, fu
end, end,
onExit = function() onExit = function()
DeletePed(Peds[randName]) DeletePed(Peds[randName])
Peds[randName] = nil
end, end,
debug = debugMode, debug = debugMode,
}) })
@@ -245,6 +247,16 @@ function GenerateRandomPedData(data)
return newTable return newTable
end end
onPlayerUnloaded(function()
for i = 1, #distPeds do
for k in pairs(Peds) do
DeletePed(Peds[k])
end
removeZoneTarget(distPeds[i])
end
distPeds = {}
end)
--- Cleans up all created Peds when the resource stops. --- Cleans up all created Peds when the resource stops.
onResourceStop(function() onResourceStop(function()
for k in pairs(Peds) do for k in pairs(Peds) do