Im trying to make it so when a player placed a Flag down zombies wont spawn like inside or near it, but i cant get it to work. This is what it looks when a player places something:
And im trying to make it check using magnitude that a zombie doesnt spawn near it, but after i added the check it doesnt event spawn it anymore:
game.Lighting.Changed:Connect(function()
if game.Lighting.ClockTime == 21 then
for i,v in pairs(game.ReplicatedStorage.DisableSpawns:GetChildren()) do
v.Parent = workspace.ZombieSpawns
end
task.wait(.1)
for count = 1,33 do
task.wait(0.4)
local clone = game.ReplicatedStorage.AI.Zombie:Clone()
local folder = game.Workspace.ZombieSpawns:GetChildren()
local rng = folder[math.random(1, #folder)]
-- start of the check
for i,v in pairs(game.Workspace.Builds:GetDescendants()) do
if v:IsA("BasePart") then
local mag = (v.Position - rng.Position).Magnitude
if mag >= 3 then
repeat
task.wait()
local rng = folder[math.random(1, #folder)]
until mag <= 3
end
end
end
-- end of the check
folder = game.Workspace.ZombieSpawns:GetChildren()
clone:SetPrimaryPartCFrame(rng.CFrame)
clone.Parent = workspace.Zombies
rng.Parent = game.ReplicatedStorage.DisableSpawns
wait(1)
rng = folder[math.random(1, #folder)]
end
end
end)
game.Lighting:GetPropertyChangedSignal("ClockTime"):Connect(function()
if game.Lighting.ClockTime ~= 21 then
return
end
for i, v in ipairs(game.ReplicatedStorage.DisableSpawns:GetChildren()) do
v.Parent = workspace.ZombieSpawns
end
task.wait(0.1)
for count = 1, 33 do
task.wait(0.4)
local clone = game.ReplicatedStorage.AI.Zombie:Clone()
local folder = workspace.ZombieSpawns:GetChildren()
local rng = folder[math.random(#folder)]
for i, v in ipairs(workspace.Builds:GetDescendants()) do
if v:IsA("BasePart") then
local distance = (v.Position - rng.Position).Magnitude
if distance < 3 then
continue
end
repeat
task.wait()
rng = folder[math.random(#folder)]
distance = (v.Position - rng.Position).Magnitude
until distance <= 3
end
end
-- end of the check
folder = workspace.ZombieSpawns:GetChildren()
clone:PivotTo(rng.CFrame)
clone.Parent = workspace.Zombies
rng.Parent = game.ReplicatedStorage.DisableSpawns
task.wait(1)
rng = folder[math.random(#folder)]
end
end)
--//Services
local ReplicatedStorage = game:GetService("ReplicatedStorage")
local Lighting = game:GetService("Lighting")
--//Functions
local function GenerateNumber(usedNumbers, maxNumber)
local newNumber = math.random(maxNumber)
while table.find(usedNumbers, newNumber) do
newNumber = math.random(maxNumber)
end
return newNumber
end
Lighting:GetPropertyChangedSignal("ClockTime"):Connect(function()
if Lighting.ClockTime ~= 21 then
return
end
for i, v in ipairs(ReplicatedStorage.DisableSpawns:GetChildren()) do
v.Parent = workspace.ZombieSpawns
end
task.wait(0.1)
for count = 1, 33 do
task.wait(0.4)
local clone = ReplicatedStorage.AI.Zombie:Clone()
local folder = workspace.ZombieSpawns:GetChildren()
local usedIndexes = {}
local isValid = false
local rng = nil
while not isValid and #usedIndexes < #folder do
local newFolderIndex = GenerateNumber(usedIndexes, #folder)
table.insert(usedIndexes, newFolderIndex)
rng = folder[newFolderIndex]
for i, descendant in ipairs(workspace.Builds:GetDescendants()) do
if not descendant:IsA("BasePart") then
continue
end
local distance = (descendant.Position - rng.Position).Magnitude
if distance < 3 then
isValid = true
else
isValid = false
break
end
end
task.wait()
end
if not isValid then
print("No place to spawn zombies (all spawns are covered by a flag)")
continue
end
-- end of the check
folder = workspace.ZombieSpawns:GetChildren()
clone:PivotTo(rng.CFrame)
clone.Parent = workspace.Zombies
rng.Parent = ReplicatedStorage.DisableSpawns
end
end)
U added some unnecessary code, I already found my solution
game.Lighting:GetPropertyChangedSignal("ClockTime"):Connect(function()
if game.Lighting.ClockTime ~= 21 then
return
end
for i, v in ipairs(game.ReplicatedStorage.DisableSpawns:GetChildren()) do
v.Parent = workspace.ZombieSpawns
end
task.wait(0.1)
for count = 1, 33 do
task.wait(0.4)
local clone = game.ReplicatedStorage.AI.Zombie:Clone()
local folder = workspace.ZombieSpawns:GetChildren()
local rng = folder[math.random(#folder)]
for i, v in ipairs(workspace.Builds:GetDescendants()) do
if v.Name == "flag" then
local distance = (v.Parent.Position - rng.Position).magnitude
if distance >= 3 then
folder = workspace.ZombieSpawns:GetChildren()
clone:PivotTo(rng.CFrame)
clone.Parent = workspace.Zombies
rng.Parent = game.ReplicatedStorage.DisableSpawns
break
end
end
end
-- end of the check
task.wait(1)
rng = folder[math.random(#folder)]
end
end)