I'm trying to make a pretty advanced AI system in my opinion but it's really behaving weird and doesn't at all move like I want it to behave.
I would want it to behave pretty much like a normal human being so in this picture I have A, B, C, D, E and I'd want it to start at A and then move to C and then maybe to E and D and so on.
And In this gif you can see how it's behaving:
https://gyazo.com/9f78158e064f8ae8fa910d02ccb41fc0
Here's my modulescript:
local AI = {}
AI.__index = AI
local player = game.Players.LocalPlayer
local Appearance = require(script.Parent.Appearance)
local Points = {}
local Last10Points = {}
local function Tablecontains(table, element)
for i,v in pairs(table) do
if table == element then
return true
end
end
return false
end
local function CreatePool()
local AssetsGuest = game.ReplicatedStorage.AssetsGuest
local IslandGuest = AssetsGuest.IslandGuest:Clone()
IslandGuest.Parent = workspace.LocalDump
IslandGuest.Name = "Bob"
local SpawnPositions = workspace.Tycoons:FindFirstChild(player.Name).Extra.Platform.SpawnPositions
local Index = math.random(1, #SpawnPositions:GetChildren())
local ChosenPoint = SpawnPositions:GetChildren()[Index]
IslandGuest:SetPrimaryPartCFrame(CFrame.new(ChosenPoint.Position))
return IslandGuest
end
function AI:CreatePath()
local PathFindingService = game:GetService("PathfindingService")
local Path = PathFindingService:FindPathAsync(self.StartTile.Position, self.TargetDestination.Position)
local Waypoints = Path:GetWaypoints()
for i = 2, #Waypoints do
local lastWaypoint = Waypoints[i - 1]
local currentWaypoint = Waypoints[i]
local segment = Instance.new("LineHandleAdornment")
local lastPosition = lastWaypoint.Position + Vector3.new(0, 0.5, 0) - self.StartTile.Position
local currentPosition = currentWaypoint.Position + Vector3.new(0, 0.5, 0) - self.StartTile.Position
local toCurrent = currentPosition - lastPosition
local distance = toCurrent.Magnitude
return currentWaypoint.Position
end
end
function AI:GetTarget()
if #Last10Points >= 10 then
print("last 10 points is more than 10 now")
Last10Points = {}
end
local Index = math.random(1, #Points)
repeat
Index = math.random(1, #Points)
wait()
until not Tablecontains(Last10Points, Points[Index])
local Point = Points[Index]
self.TargetDestination = Point
self.StartTile = self.RootPart
if (self.TargetDestination.Position - self.RootPart.Position).magnitude > 10 then
return self.TargetDestination, Index
else
repeat
Index = math.random(1, #Points)
wait()
print("waiting")
until not Tablecontains(Last10Points, Points[Index])
print("seccond man choise duuuddee")
local Point = Points[Index]
self.TargetDestination = Point
self.StartTile = self.RootPart
return self.TargetDestination, Index
end
end
function AI:New(Tycoon)
local self = {
Tycoon = Tycoon,
Model = CreatePool(),
-- Name = Name,
RootPart = nil,
Humanoid = nil,
StartTile = nil,
LastTarget = nil,
TargetDestination = nil,
TimeElapsed = tick(),
TimeUntilDestination = 0,
STATE_OCCUPIED = 0,
STATE_QUEUED = 0,
STATE_WANDERING = 0,
STATE_IDLE = 0,
}
setmetatable(self, AI)
for i,v in pairs(workspace:GetDescendants()) do
if v.Name == "Point" then
if not Tablecontains(Points, v) then
v.Transparency = 0.5
table.insert(Points, v)
end
end
end
self.RootPart = self.Model:FindFirstChild("HumanoidRootPart")
self.Humanoid = self.Model:FindFirstChild("Humanoid")
self:Spawn()
end
function AI:Spawn()
local STATE_OCCUPIED = self.STATE_OCCUPIED
local STATE_QUEUED = self.STATE_QUEUED
local STATE_WANDERING = self.STATE_WANDERING
if STATE_WANDERING == 0 then
self:Roam()
else
print("NPC already wandering")
end
end
function AI:Roam()
local STATE_OCCUPIED = self.STATE_OCCUPIED
local STATE_QUEUED = self.STATE_QUEUED
local STATE_WANDERING = self.STATE_WANDERING
local STATE_IDLE = self.STATE_IDLE
while (true) do
if STATE_OCCUPIED == 0 and STATE_QUEUED == 0 and STATE_WANDERING == 0 then
STATE_WANDERING = 1
local Target, Index = self:GetTarget()
local Position = self:CreatePath()
if not Position then
repeat
wait()
Position = self:CreatePath()
until Position
end
self.Humanoid:MoveTo(Position)
self.Humanoid.MoveToFinished:Wait()
if (self.RootPart.Position - self.TargetDestination.Position).magnitude < 6 then
table.insert(Last10Points, Points[Index])
end
STATE_WANDERING = 0
if STATE_OCCUPIED == 0 and STATE_QUEUED == 0 and STATE_WANDERING == 0 then
STATE_IDLE = 1
end
end
wait(.1)
end
end
return AI
