Hello! I’m trying to make an ai script which will spawn nearby a player (queen) for each of the three colonies.
This will be decided as every 5 ants it finds(players) it will spawn an ai nearby, by running the function. I am also wanting it to check if there are AI nearby the queen and add them, this way there won’t be excessive AI. I also want a maximum amount of AI nearby someone. I made this script and it seems to have issues. Anyone else know a better way to do this or if they could fix up my script? It would be much appreciated thanks!
https://gyazo.com/26f5693f8da02df41d8d4ea69dbe3a7b (the player characters are stored in here to define what class/colony they are.
local ants = workspace.Ant
local aiFolder = game.ServerStorage.AI
local aiFactor = 5 --for every _ ants an ai will spawn
local function spawnAI(player)
local humanoidRootPart = player.Character.HumanoidRootPart -- wherever it is located
local AI = game.ServerStorage.AI.Larvae:Clone() -- wherever it is located
local direction = math.random(1, 360)
local magnitude = math.random(100, 300) -- this is just a distance value i.e. how far away from the player
AI:SetPrimaryPartCFrame(humanoidRootPart.CFrame * CFrame.Angles(0, direction, 0))
AI:SetPrimaryPartCFrame(humanoidRootPart.CFrame * CFrame.new(0, 0, -magnitude))
AI.Parent = workspace.AI
end
for _,colony in pairs(ants:GetChildren()) do
for i,class in pairs(colony:GetChildren()) do
if class.Name ~= "Eggs" or class.Name ~= "Larvae" then
local numofants = 0
class.ChildAdded:Connect(function()
numofants = 0
wait()
for i,class in pairs(colony:GetChildren()) do
for _,ant in pairs (class:GetChildren()) do
local Queen = game.Players:GetPlayerFromCharacter(colony.Queen:FindFirstChildWhichIsA("Model"))
if ant:FindFirstChild("Humanoid") then -- if the ant is a character
numofants = numofants + 1
local numofAI = 0
local nearbyAI = 0
numofAI = numofants/aiFactor
for _,ai in pairs(workspace.AI:GetChildren()) do
local Distance = Queen:DistanceFromCharacter(ai.HumanoidRootPart.Position)
if Distance <= 300 then
numofAI = numofAI - 1
end
end
print(numofants)
if numofAI >= 1 then
for _ = 0, numofAI do
spawnAI(Queen)
print(numofants)
end
end
end
end
end
end)
end
end
end