I use this but it still does't work very well. Can someone help.


local myHuman = script.Parent:WaitForChild("Humanoid")
local myTorso = script.Parent:WaitForChild("HumanoidRootPart")


local clone = script.Parent:Clone()
local attackCool = true
local pathFindFails = 0
local charging = false

function checkSight(target)
	local ray = Ray.new(myTorso.Position, (target.Position - myTorso.Position).Unit * 200)
	local hit,position = workspace:FindPartOnRayWithIgnoreList(ray, {script.Parent})
	if hit then
		if hit:IsDescendantOf(target.Parent) then
			return true
		end
	end
	return false
end

function findTarget()
	local dist = 300
	local target = nil
	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 (myTorso.Position - torso.Position).magnitude < dist and human.Health > 0 then
				target = torso
				dist = (myTorso.Position - torso.Position).magnitude
			end
		end
	end
	return target
end

function pathToTarget(target)
	local path = game:GetService("PathfindingService"):CreatePath()
	path:ComputeAsync(myTorso.Position, target.Position)
	if path.Status == Enum.PathStatus.Success then
		pathFindFails = 0
		local waypoints = path:GetWaypoints()
		for i,v in ipairs(waypoints) do
			if v.Action == Enum.PathWaypointAction.Jump then
				myHuman.Jump = true
			end
			myHuman:MoveTo(v.Position)
			if checkSight(target) and math.abs(myTorso.Position.Y - target.Position.Y) < 1.5 then
				break
			elseif (target.Position - waypoints[#waypoints].Position).magnitude > 30 then
				break
			elseif math.abs(myTorso.Position.Y - v.Position.Y) > 10 then
				break
			elseif (myTorso.Position - v.Position).magnitude > 10 then
				break
			end
			if i % 5 ==0 then
				if findTarget() ~= target then
					break
				end
			end
			spawn(function()
				wait(1)
				if myTorso.Position.Y < myHuman.WalkToPoint.Y and 
					(myTorso.Position - target.Position).magnitude > 10 and charging == false then
					myHuman.Jump = true
				end
			end)
		end
	else
		pathFindFails = pathFindFails + 1 
		if pathFindFails > 10 then
			getUnstuck()
			pathFindFails = 0
		end
	end
end

function walkRandom()
	local xRand = math.random(-50,50)
	local zRand = math.random(-50,50)
	local goal = myTorso.Position + Vector3.new(xRand,0,zRand)
	
	local path = game:GetService("PathfindingService"):CreatePath()
	path:ComputeAsync(myTorso.Position, goal)
	
	if path.Status == Enum.PathStatus.Success then
		local waypoints = path:GetWaypoints()
		for i,v in ipairs(waypoints) do
			if v.Action == Enum.PathWaypointAction.Jump then
				myTorso.Jump = true
			end
			myHuman:MoveTo(v.Position)
			myHuman.MoveToFinished:Wait()
		end
	else
		wait(2)
	end
end

function getUnstuck()
	myHuman:Move(Vector3.new(math.random(-1,1),0,math.random(-1,1)))
	wait(1)
end

function chase(target)
	charging = true
	myHuman:MoveTo(target.Position)
end

myHuman.Died:Connect(function()
	wait(15)
	clone.Parent = workspace
	game:GetService("Debris"):AddItem(script.Parent,0.1)
end)

function main()
	charging = false 
	local target = findTarget()
	if target then
		if checkSight(target) and math.abs(myTorso.Position.Y - target.Position.Y) < 1.5 or
			checkSight(target) and (myTorso.Position - target.Position).magnitude < 3 then
			local dist = (myTorso.Position - target.Position).magnitude 
			if dist > 3 then
				myHuman.WalkSpeed = 12 
				chase(target)
			end
		else
			myHuman.WalkSpeed = 10
			pathToTarget(target)
		end		
	else
		walkRandom()
	end
end

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

2 comments

0

Can you explain what Is wrong?

0

The script is a bit repetitive, i could improve it a bit