Hello guys, I have a doubt and asking for help for those who understand!
The AI works, but it has some bugs, sometimes the enemy falls when I arrive, I would also like that when the player leaves the enemy’s function it activates something like a print and I can put a command on it with wait without it this time to walk, and wait for the enemy movement is a little strange.
IA Script:
local myHuman = script.Parent:WaitForChild("Humanoid")
local myRoot = script.Parent:WaitForChild("HumanoidRootPart")
local head = script.Parent:WaitForChild("Head")
local lowerTorso = script.Parent:WaitForChild("LowerTorso")
local clone = script.Parent:Clone()
local folder = script.Parent.HittedPlayers
local attackAnimation = myHuman:LoadAnimation(script:WaitForChild("AttackAnimation"))
local distSee = 30
local distAttack = 4
local timeAttack = 1
local damage = 8
function findPath(target)
local path = game:GetService("PathfindingService"):CreatePath()
path:ComputeAsync(myRoot.Position,target.Position)
local waypoints = path:GetWaypoints()
if path.Status == Enum.PathStatus.Success then
for _, waypoint in ipairs(waypoints) do
if waypoint.Action == Enum.PathWaypointAction.Jump then
myHuman.Jump = true
end
myHuman:MoveTo(waypoint.Position)
local timeOut = myHuman.MoveToFinished:Wait(1)
if not timeOut then
myHuman.Jump = true
--print("Caminho muito longo!")
findPath(target)
break
end
if checkSight(target) then
repeat
--print("Movendo-se diretamente para o alvo")
if (myRoot.Position - target.Position).magnitude > distAttack then
myHuman:MoveTo(target.Position)
elseif (myRoot.Position - target.Position).magnitude <= distAttack then
myHuman:MoveTo(myRoot.Position)
attack(target)
end
wait(0.1)
if target == nil then
break
elseif target.Parent == nil then
break
end
until checkSight(target) == false or myHuman.Health < 1 or target.Parent.Humanoid.Health < 1
break
end
if (myRoot.Position - waypoints[1].Position).magnitude > distSee then
--print("O alvo mudou, gerando um novo caminho")
findPath(target)
break
end
end
end
end
function checkSight(target)
local ray = Ray.new(myRoot.Position, (target.Position - myRoot.Position).Unit * 40)
local hit,position = workspace:FindPartOnRayWithIgnoreList(ray, {script.Parent})
if hit then
if hit:IsDescendantOf(target.Parent) and math.abs(hit.Position.Y - myRoot.Position.Y) < 3 then
--print("Eu posso ver o alvo")
return true
end
end
return false
end
function findTarget()
local target = nil
local potentialTargets = {}
local seeTargets = {}
for i,v in ipairs(workspace:GetChildren()) do
local human = v:FindFirstChild("Humanoid")
local torso = v:FindFirstChild("Torso") or v:FindFirstChild("HumanoidRootPart")
if human and torso and v.Name ~= script.Parent.Name then
if (myRoot.Position - torso.Position).magnitude < distSee and human.Health > 0 then
table.insert(potentialTargets,torso)
else
myHuman:MoveTo(script.Spawn.Position)
-- folder:ClearAllChildren()
end
end
end
if #potentialTargets > 0 then
for i,v in ipairs(potentialTargets) do
if checkSight(v) then
table.insert(seeTargets, v)
elseif #seeTargets == 0 and (myRoot.Position - v.Position).magnitude < distSee then
target = v
distSee = (myRoot.Position - v.Position).magnitude
end
end
end
if #seeTargets > 0 then
for i,v in ipairs(seeTargets) do
if (myRoot.Position - v.Position).magnitude < distSee then
target = v
distSee = (myRoot.Position - v.Position).magnitude
end
end
end
if target then
if math.random(10) == 1 then
end
end
return target
end
function attack(target)
if target.Parent ~= nil then
attackAnimation:Play()
myRoot.CFrame = CFrame.new(myRoot.Position, target.Position)
target.Parent.Humanoid:TakeDamage(damage)
end
wait(timeAttack)
end
function died()
wait(5)
clone.Parent = workspace
game:GetService("Debris"):AddItem(script.Parent,0.1)
end
myHuman.Died:Connect(died)
lowerTorso.Touched:Connect(function(obj)
if not obj.Parent:FindFirstChild("Humanoid") then
myHuman.Jump = true
end
end)
function main()
local target = findTarget()
if target then
myHuman.WalkSpeed = 16
findPath(target)
else
myHuman.WalkSpeed = 8
end
end
while wait(0.01) do
if myHuman.Health < 1 then
break
end
main()
end