[SOLVED] My Flying Boss doesn't attack me

I need help with my boss as it doesn’t want to attack me or do anything. Its got a custom collision group but I don’t know if that affects anything…

SCRIPT:

local PathfindingService = game:GetService("PathfindingService")
local Debris = game:GetService("Debris")

local Infernal = script.Parent
local HRP = Infernal:WaitForChild("HumanoidRootPart")
local BodyPosition = HRP:WaitForChild("BodyPosition")

local Humanoid = Infernal:WaitForChild("Humanoid")

local Effects = workspace:WaitForChild("Effects")

BodyPosition.Position = HRP.Position + Vector3.new(0, script.Height.Value, 0) + Vector3.new(0, math.random(-1.5,1.5), 0)

local Waypoints = workspace:WaitForChild("BossWaypoints")

--[[
while true do
	task.wait(1)
	BodyPosition.Position = HRP.Position + Vector3.new(0, -5, 0)
	task.wait(1)
	BodyPosition.Position = HRP.Position + Vector3.new(0, 5, 0)
end
]]

local rayParams = RaycastParams.new()
rayParams.FilterType = Enum.RaycastFilterType.Blacklist
rayParams.FilterDescendantsInstances = {Infernal}

local pathParams = {
	AgentHeight = 5,
	AgentRadius = 3,
	AgentCanJump = true,
}

local lastPos
local RANGE = 999999999999999

local function PlaySound(ID, Volume)
	local Sound = Instance.new("Sound", Effects)
	Sound.Volume = Volume
	Sound.SoundId = "rbxassetid://" .. ID
	Sound.Loaded:Wait()
	Sound:Play()
	Debris:AddItem(Sound, Sound.TimeLength + 0.1)
end

local function canSeeTarget(target)
	local orgin = HRP.Position
	local direction = (target.HumanoidRootPart.Position - HRP.Position).Unit * RANGE
	local ray = workspace:Raycast(orgin, direction, rayParams)

	if ray and ray.Instance then
		if ray.Instance:IsDescendantOf(target) then
			return true
		else
			return false
		end
	else
		return false
	end
end

local function findTarget()
	local players = game.Players:GetPlayers()
	local maxDistance = RANGE
	local nearestTarget

	for i, player in pairs(players) do
		if player.Character then
			local target = player.Character
			local distance = (HRP.Position - target.HumanoidRootPart.Position).Magnitude

			if distance < maxDistance and canSeeTarget(target) then
				nearestTarget = target
				maxDistance = distance
			end
		end
	end

	return nearestTarget
end

local function getPath(destination)
	local path = PathfindingService:CreatePath(pathParams)

	path:ComputeAsync(HRP.Position, destination.Position)

	return path	
end

local function Charge(target)
	local distance = (HRP.Position - target.HumanoidRootPart.Position).Magnitude
	local debounce = false
	
	if distance > RANGE then
		BodyPosition.Position = target.HumanoidRootPart.Position
		PlaySound(7267837723, 1)
		task.wait(2)
	end
end

local function walkTo(destination)
	local path = getPath(destination)

	if path.Status == Enum.PathStatus.Success then
		for i, waypoint in pairs(path:GetWaypoints()) do
			path.Blocked:Connect(function()
				path:Destroy()
			end)

			local target = findTarget()

			if target and target.Humanoid.Health > 0 then
				lastPos = target.HumanoidRootPart.Position
				Charge(target)
				break
			else
				if waypoint.Action == Enum.PathWaypointAction.Jump then
					Humanoid:ChangeState(Enum.HumanoidStateType.Jumping)
				end

				if lastPos then
					BodyPosition.Position = lastPos
					task.wait(1)
					lastPos = nil
					break
				else
					BodyPosition.Position = waypoint.Position
					task.wait(1)
				end
			end
		end
	else
		return
	end
end

local function patrol()
	local waypoints = Waypoints:GetChildren()
	local randomNum = math.random(1, #waypoints)
	walkTo(waypoints[randomNum])
end

task.spawn(function()
	while task.wait(0.2) do
		patrol()
	end
end)

Can you show us a video of the bug happening? What is your script suppose to do and what it’s doing instead? Can you provide any errors/outputs shown in the output tab?

Boss Purpose: If you have played a game called Terraria then this should be familar: It acts similar to the Eye Of Cthulu which charges at the player. The Boss I have created does the same thing. It sort of flies at the player in hoping to damage them. The Boss also has 0 collision with the environment letting it flow freely without any problems.

The Script instead proceeds to do this:

HOWEVER!

THE SCRIPT WORKED SLIGHTLY! The bossed dashed at the player like I wanted it to except it stops after 2 dashes as it collides with the ground and simply just stops there.

BUT!

The script either does 1 of 2 things. Actually working for a short while before it stops. Or just flying into the stratosphere for no reason. There are also no errors for the script.

Current Problems: The boss collision seems to be weird, it starts to be able to touch the baseplate even though my CollisionGroup looks like this:
image
And of course the boss AI.

I think the root cause lies within how the path is created. If the path was not created successfully, then the boss will not move. One reason why the path may not be able to be created is because the path is blocked by obstacles.

There are a lot of things happening in the script which I can’t go through all of them, but here’s what I can suggest. For every step successfully carried out by the script, I want you to use a print statement to indicate that step is done and carried out successfully. Do this for every step in the script and find the print statements which did not fire. Those steps are the ones that fail to carry out its function and you can inspect the problem from there.

sorry my music was on, lol

I think now the problem is that when it has found a target, it still keeps on patrolling which it shouldn’t. Try to make it not patrol when it has found a target until the target is lost.

I have to sleep now, I hope you are able to solve the problem by your own with other people’s help. Good luck!

Thank you!

placeholderplaceholderplaceholder

EDIT: I managed to fix it by doing some simple fixes, it was a long and tedious progress but I managed to get there after trial and error.