Basically I have been trying to create an AI script but I haven’t been able to make the AI detect the Players positions changed then change direction to follow it, Assistance is needed.
This is the script:
local pathfindingService = game:GetService("PathfindingService")
local humanoid = script.Parent.Humanoid
local body = script.Parent:FindFirstChild("HumanoidRootPart") or script.Parent:FindFirstChild("Torso") or script.Parent:FindFirstChild("Top Belly")
local change = false
local debounce = false
local bounce = false
local players = {}
function WaitFor(Root, Char)
body.Touched:Connect(function()
humanoid.Jump = true
end)
end
function MoveToPlayer(Root, Char)
local path = pathfindingService:CreatePath()
local points = {}
table.insert(points, body.Position)
local destination = game.Workspace:WaitForChild("Inc_X"):WaitForChild("Torso").Position
path:ComputeAsync(body.Position, destination)
local waypoints = path:GetWaypoints()
wait(.01)
if path.Status == Enum.PathStatus.Success then
for i, p in pairs(waypoints) do
local pos = p.Position
table.insert(points, pos)
for i, point in pairs(points) do
if pos == i then
table.remove(points, point)
else
end
end
end
for point = 1, #points do
humanoid:MoveTo(points[point])
humanoid.MoveToFinished:Wait(1)
WaitFor()
end
else
if bounce == false then
bounce = true
MoveToPlayer()
wait(5)
bounce = false
end
end
MoveToPlayer()
end
MoveToPlayer()
I will give you a huge tip for AI. All AI’s involve sensory. To have an effective AI it needs lots of sensory things. Think of what you do when you play roblox. You listen, you see, you react, you analyze and take courses of action in your head.
Depending on the AI (maybe you want an uber intelligent one), you’ll take different courses. If you’re making a learning AI, a neural network might be wise, and while I’ve not seen one done on roblox, I’d love to see it tried. If you want a basic movement AI, sensors are important.
Think of magnitudes and how roblox players react. If a player is closest to a player and attacking, usually a player attacks them. If they’re already attacking a player, usually they don’t end up switching to a new opponent unless they are in the field of view and can see that they’re being attacked. Otherwise they assume its damage from someone else.
Doesn’t look like you use a lot of sensory in your AI code, so try using more of that.
While I didn’t give you and code, I do hope I gave you some food for thought.