AI Code Review [Feedback]

Using this video https://www.youtube.com/watch?v=ibvoqnG3YqI , I was able to make an AI (I tinkered with the code a lot so it’s not the same). I was looking for feedback since this is my first time ever working with AIs.

local myHuman = script.Parent:WaitForChild("Humanoid")
local myRoot = script.Parent:WaitForChild("HumanoidRootPart")
local head = script.Parent:WaitForChild("Head")
local lowerTorso = script.Parent:WaitForChild("LowerTorso")
Targets = {}

local grab = script:WaitForChild("Grab")
local grabAnim = myHuman:LoadAnimation(grab)
grabAnim.Priority = Enum.AnimationPriority.Action

local grab2 = script:WaitForChild("Grab2")
local grabAnim2 = myHuman:LoadAnimation(grab2)
grabAnim2.Priority = Enum.AnimationPriority.Action

local grabSound = head:WaitForChild("Attack")
local screamSound = head:WaitForChild("Scream")

local clone = script.Parent:Clone()


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()
			if not timeOut then
				myHuman.Jump = true
				--Path too long
				findPath(target)
				break
			end
			if checkSight(target) then
				repeat wait()
					--Moving directly to the target
					myHuman:MoveTo(target.Position)
					attack(target)
					if target == nil then
						break
					elseif target.Parent == nil then
						break
					end
				until false or myHuman.Health < 1 or target.Parent.Humanoid.Health < 1
				break
			end
			if (myRoot.Position - waypoints[1].Position).magnitude > 20 then
				--Target has moved, generating new path")
				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
			--I can see the target
			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("HumanoidRootPart") or v:FindFirstChild("HumanoidRootPart")
		if human and torso and v.Name ~= script.Parent.Name  then
			if _G.SafePlayers == nil then
				--SafePlayers are players who are in a certain area, I don't want them to be chased
					if human.Health > 0 then
					table.insert(potentialTargets,torso)
				elseif table.find(_G.SafePlayers,human.Parent) then
					if human.Health > 0 then
					table.insert(potentialTargets,torso)
					end
				end
			end
		end
	end
	if #potentialTargets > 0 then
		for i,v in ipairs(potentialTargets) do
			if checkSight(v) then
				table.insert(seeTargets, v)
				target = v
			end
		end
	end
	if #seeTargets > 0 then
		for i,v in ipairs(seeTargets) do
		end
	end
	return target
end

function attack(target)
	if (myRoot.Position - target.Position).magnitude < 5 then
		grabSound:Play()
		if target.Parent ~= nil then
			local Character = target.Parent
			local humanoid = target.Parent.Humanoid
    		if Targets[Character] == nil or tick() - Targets[Character] > 3 then
				Targets[Character] = tick()
				local random = math.random(1,2)
				print(random)
				if random == 1 then
					grabAnim:Play()
				else 
					grabAnim2:Play()
				end
				humanoid:TakeDamage(25)
			end
		end
	end
end

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 = 20
		findPath(target)
	end
end

while wait() do
	if myHuman.Health < 1 then
		break
	end
	main()
end
1 Like

Maybe you should move it to

Oh, I meant to put it there, Sorry!

I’d learn to use finite state machines and OOP programming as it could be super useful for these types of things.