I can up with this, and it works, but it seems really complicated. Is there an easier way:
local function FindSafestSpawner(player)
local enemyDistances = {}
local savedDistances = {}
for _, spawner in pairs(spawns:GetChildren()) do
for _, zombie in pairs(game.Workspace.Zombies:GetChildren()) do
table.insert(enemyDistances, (spawner.Position - zombie.PrimaryPart.Position).Magnitude)
end
local closestDistance = math.min(unpack(enemyDistances))
spawner.Distance.Value = closestDistance
table.insert(savedDistances, closestDistance)
end
local farthest = math.max(unpack(savedDistances))
local spawned = false
for _, spawner in pairs(spawns:GetChildren()) do
if spawner.Distance.Value == farthest then
player.RespawnLocation = spawner
player:LoadCharacter()
spawned = true
end
end
if spawned == false then
player.RespawnLocation = game.Workspace.Spawns.Default
player:LoadCharacter()
end
end
you can make a function to find the smallest (or largest) index, which is cleaner and also saves alot of memory
local function getSmallestIndex(t : {number}) : number
local smallestIndex = 1
local smallestValue = t[1]
for i = 2, #t do
if t[i] < smallestValue then
smallestIndex = i
smallestValue = t[i]
end
end
return smallestIndex
end
now you dont have to iterate again through spawns and then compare the distance; you can just find the index one time and directly get the associated spawner
local spawners = spawns:GetChildren()
local spawnerDistances = {} -- you would fill this with distances like you already do
local nearestSpawnerIndex = getSmallestIndex(spawnerDistances)
local nearestSpawner = spawnerDistances[nearestSpawnerIndex] -- direct indexing instead of iterating to find
-- and then you can do the same thing with farthest
If have found that: local closestDistance = math.min(unpack(enemyDistances))
produces the same result as the function:
local function getSmallestIndex(t : {number}) : number
local smallestIndex = 1
local smallestValue = t[1]
for i = 2, #t do
if t[i] < smallestValue then
smallestIndex = i
smallestValue = t[i]
end
end
return smallestIndex
end
It seems easier to just use min/max “unpack” table for that part.
Let me know if I am not understanding your suggestion.
Also, I need the actual Instance of the spawner, not just the smallest number (closest zombie) for each spawner.
I would still like to know if there is an easier way to find the “SAFEST” spawner.
But, in the meantime, I found a simple way to find a “SAFE” spawner by checking if the closest zombie is at least 20 studs way.
local function FindSafeSpawner(player)
local safeSpawner = nil
for _, spawner in pairs(spawns:GetChildren()) do
local distances = {}
for _, zombie in pairs(zombies:GetChildren()) do
local distance = (spawner.Position - zombie.PrimaryPart.Position).Magnitude
table.insert(distances, distance)
end
local closest = math.min(unpack(distances))
if closest >= 20 then
safeSpawner = spawner
break
end
end
if safeSpawner then
player.RespawnLocation = safeSpawner
else
player.RespawnLocation = spawns.Default
end
end
Why are you storing the zombie positions in a table? Couldn’t you just do this:
local function FindSafeSpawner(player)
local safeSpawner = nil
for _, spawner in pairs(spawns:GetChildren()) do
for _, zombie in pairs(zombies:GetChildren()) do
local distance = (spawner.Position - zombie.PrimaryPart.Position).Magnitude
if distance >= 20 then
safeSpawner = spawner
break
end
end
if safeSpawner then break end
end
player.RespawnLocation = safeSpawner or spawns.Default
end
I put them in a table so I could then pull out the closest zombie to the spawner. If the closest zombie is within 20 studs of the spawner, then it is not a safe spawner.
Your sample does not appear to account for all zombies near the spawner. It just looks to see if any zombie in the game is >20 studs away.
There was a logic mistake in the code that I hadn’t noticed, sorry. What I meant to do was remove the use of tables for this. It’s more efficient, but a bit more verbose.
local function FindSafeSpawner(player)
local safeSpawner = spawns.Default
for _, spawner in pairs(spawns:GetChildren()) do
local unsafe = false
for _, zombie in pairs(zombies:GetChildren()) do
local distance = (spawner.Position - zombie.PrimaryPart.Position).Magnitude
if distance <= 20 then
unsafe = true
break
end
end
if not unsafe then
safeSpawner = spawner
break
end
end
player.RespawnLocation = safeSpawner
end