You can write your topic however you want, but you need to answer these questions:
-
What do you want to achieve? Keep it simple and clear!
I am making a game where ai npc battle each other with random weapons, the problem is that handling a lot of npc causes a lot of lag, and i thought about using a single script that will handle each npc. -
What is the issue? Include screenshots / videos if possible!
i dont know how i can implement this -
What solutions have you tried so far? Did you look for solutions on the Developer Hub?
I tried looking for something similar and found about using collection service but i have no idea how can i use it
heres the code i tried so far
-------Services-------
local AssetFolder = game.ReplicatedStorage:WaitForChild("Assets")
local PathFindingService = game:GetService("PathfindingService")
local AiModule = require(AssetFolder.Modules.AiModule)
local Tags = game:GetService("CollectionService")
local PathfindingService = game:GetService("PathfindingService")
------Parameters---------
local Bot_Tag = "Bots"
local viewRange = 30
local fieldOfView = 70
local Path = PathFindingService:CreatePath(
{
AgentRadius = 4;
AgentHeight = 5;
AgentCanJump = true;
Costs = {
FreeZone = math.huge
};
}
)
function FollowPosition(Position : Vector3,waypoints, BlockConnection, NextwaypointIndex, ReachConnection, Humanoid)
local Sucsess, ErrorMessage = pcall(function()
Path:ComputeAsync(script.Parent.HumanoidRootPart.Position, Position)
end)
if Sucsess and Path.Status == Enum.PathStatus.Success then
waypoints = Path:GetWaypoints()
BlockConnection = Path.Blocked:Connect(function(blockedwaypointindex)
if blockedwaypointindex > NextwaypointIndex then
BlockConnection:Disconnect()
FollowPosition(Position, waypoints, BlockConnection, NextwaypointIndex, ReachConnection)
end
end)
if not ReachConnection then
ReachConnection = Humanoid.MoveToFinished:Connect(function(Reached)
if Reached and NextwaypointIndex < #waypoints then
NextwaypointIndex += 1
Humanoid:MoveTo(waypoints[NextwaypointIndex].Position)
end
end)
end
NextwaypointIndex = 2
Humanoid:MoveTo(waypoints[NextwaypointIndex].Position)
task.wait()
end
end
function setupNpc(Bot)
print("Test Loop")
--start AI loop for this NPC
--other NPC stuff
spawn(function()
local waypoints
local NextwaypointIndex
local ReachConnection
local BlockConnection
while task.wait() do
local Target = AiModule:FindTargetForBot(Bot)
if Target then
Bot.Data.Target.Value = Target
FollowPosition(Target:FindFirstChild("HumanoidRootPart").Position, waypoints, BlockConnection, ReachConnection, Target:FindFirstChild("Humanoid"))
end
end
end)
end
function cleanupNpc(Bot)
--free resources used by the NPC
end
game.ReplicatedStorage.Remotes.Server.SetupAi.Event:Connect(function()
for _, Bot in ipairs(workspace.Bots:GetChildren()) do
setupNpc(Bot)
end
Tags:GetInstanceAddedSignal(setupNpc)
Tags:GetInstanceRemovedSignal(cleanupNpc)
end)
Please do not ask people to write entire scripts or design entire systems for you. If you can’t answer the three questions above, you should probably pick a different category.