NPC randomly vanishes

i have made this boss, and i gave it a phase 2, and ever since that it started randomly vanishing

i have NO idea why this happens and i cant seem to find out how to fix this

it seems to happen at random times, it mostly happens when i respawn, so i think it may be something with him finding a new target?

example video: (you can see when he vanishes right when i respawn)

heres the script

local myHuman = script.Parent:WaitForChild("Humanoid")
local myRoot = script.Parent:WaitForChild("HumanoidRootPart")
local head = script.Parent:WaitForChild("Head")
local lowerTorso = script.Parent:WaitForChild("Torso")
local boss = myHuman.Parent

local hmrp = myRoot
local humanoid = myHuman

local animator = myHuman.Animator

local immunites = boss["ragdoll/stun"].immunities

local npcEvent = game:GetService("ReplicatedStorage").remoteEvents.changeNpcState
npcEvent:FireAllClients(myHuman, Enum.HumanoidStateType.FallingDown, false, "set")
npcEvent:FireAllClients(myHuman, Enum.HumanoidStateType.Ragdoll, false, "set")
myHuman:SetStateEnabled(Enum.HumanoidStateType.FallingDown, false)
myHuman:SetStateEnabled(Enum.HumanoidStateType.Ragdoll, false)

-----------------//BOSS VARIABLES//-----------------

local stopDistance = 3

local walkspeedBase = 12
local runSpeed = 18
local jumppowerBase = 50
local maxDistance = 10000

local isCorner = false
local aggrovated = false
local isRunning = false
local cornerDistance = 0.35

local attackCooldown = 0
local screamCooldown = 0

local clawDMG = 48

local PHASE2ACTIVE = false

-----------------//ANIMATIONS//-----------------

local sitAnim = animator:LoadAnimation(script.sit)
local sitLookAnim = animator:LoadAnimation(script.sitLook)
local aggrovateStartAnim = animator:LoadAnimation(script.aggrovateStart)
local aggrovatingAnim = animator:LoadAnimation(script.aggrovating)
local aggrovateEndAnim = animator:LoadAnimation(script.aggrovateEnd)
local claw1Anim = animator:LoadAnimation(script.claw1)
local claw2Anim = animator:LoadAnimation(script.claw2)
local dashAnim = animator:LoadAnimation(script.dash)
local dashGrabAnim = animator:LoadAnimation(script.dashGrab)
local screamAnim = animator:LoadAnimation(script.scream)
local throwGrabAnim = animator:LoadAnimation(script.grabthrow)
local throwAnim = animator:LoadAnimation(script.throw)
local shriekAnim = animator:LoadAnimation(script.shriek)
local phase2startAnim = animator:LoadAnimation(script.phase2start)
local slamAnim = animator:LoadAnimation(script.slam)

local walkspeed = boss.walkspeedSet
local jumppower = boss.jumppowerSet

walkspeed.Value = walkspeedBase
jumppower.Value = jumppowerBase

walkspeed.Changed:Connect(function()
	myHuman.WalkSpeed = walkspeed.Value
end)
jumppower.Changed:Connect(function()
	myHuman.JumpPower = jumppower.Value
end)

myHuman.WalkSpeed = walkspeed.Value
myHuman.JumpPower = jumppower.Value

local checkTeamEvent = game:GetService("ReplicatedStorage").bindableFunctions.checkSameTeam
local debris = game:GetService("Debris")
local ts = game:GetService("TweenService")

local hitboxModule = require(game:GetService("ReplicatedStorage").hitboxModule)
local dmgEvent = game:GetService("ReplicatedStorage").bindableEvents.damagePlayer
local kbEvent = game:GetService("ReplicatedStorage").bindableEvents.knockbackPlayer
local checkStunned = game:GetService("ReplicatedStorage").bindableFunctions.checkStunned
local hitboxModule = require(game:GetService("ReplicatedStorage").hitboxModule)

local function solveQuadratic(a,b,c)
	-- https://en.wikipedia.org/wiki/Quadratic_formula
	local discriminant = b^2 - 4*a*c;
	local numerator = -(b + discriminant^0.5);
	local denom = 2 * a;
	return numerator / denom;
end

local function solvePrediction(speed, targetPosition, targetVelocity, shooterPosition)
	local relative = targetPosition - shooterPosition; 
	local angle = math.acos(relative.Unit:Dot(targetVelocity.Unit))

	local a,b,c;
	a = targetVelocity.magnitude^2 - speed^2
	b = -2 * relative.magnitude * math.cos(angle) * targetVelocity.magnitude;
	c = relative.magnitude^2;

	local t = solveQuadratic(a,b,c);
	return targetPosition + targetVelocity * t;
end

local filter2 = {workspace.fx}
local function updateFilter2(part: Part)
	if part then
		if part.Parent then
			if part:IsA("BasePart") then
				if part.CanCollide == false then
					if not part:IsDescendantOf(workspace.fx) then
						if not part.Parent:FindFirstChildWhichIsA("Humanoid") then
							if not part:IsDescendantOf(workspace.fx) then
								table.insert(filter2, part)
							end
						else
							table.insert(filter2, part)
						end
					end
				else
					if part.Parent:FindFirstChildWhichIsA("Humanoid") then
						table.insert(filter2, part)
					elseif part.Parent:IsA("Accessory") then
						table.insert(filter2, part)
					end
				end
			end
		end
	else
		filter2 = {workspace.fx}
		for i, v in pairs(workspace:GetDescendants()) do
			if v then
				if v.Parent then
					if v:IsA("BasePart") then
						if v.CanCollide == false then
							if not v:IsDescendantOf(workspace.fx) then
								if not v.Parent:FindFirstChildWhichIsA("Humanoid") then
									if not v:IsDescendantOf(workspace.fx) then
										table.insert(filter2, v)
									end									
								else
									table.insert(filter2, part)
								end
							end
						else
							if v.Parent:FindFirstChildWhichIsA("Humanoid") then
								table.insert(filter2, v)
							elseif v.Parent:IsA("Accessory") then
								table.insert(filter2, v)
							end
						end
					end
				end
			end
		end
	end
end

updateFilter2(nil)

workspace.DescendantAdded:Connect(function(desc)
	updateFilter2(desc)
end)

local filter = {boss, workspace.fx}
local function updateFilter(part: Part)
	if part then
		if part:IsA("BasePart") then
			if part.CanCollide == false then
				if not part:IsDescendantOf(workspace.fx) then
					if not part.Parent:FindFirstChildWhichIsA("Humanoid") then
						if not part:IsDescendantOf(workspace.fx) then
							table.insert(filter, part)
						end
					end
				end
			end
		end
	else
		filter = {boss, workspace.fx}
		for i, v in pairs(workspace:GetDescendants()) do
			if v:IsA("BasePart") then
				if v.CanCollide == false then
					if not v:IsDescendantOf(workspace.fx) then
						if not v.Parent:FindFirstChildWhichIsA("Humanoid") then
							if not v:IsDescendantOf(workspace.fx) then
								table.insert(filter, v)
							end
						end
					end
				end
			end
		end
	end
end

updateFilter(nil)

workspace.DescendantAdded:Connect(function(desc)
	updateFilter(desc)
end)

local function playAnim(anim: AnimationTrack, fade)
	sitAnim:Stop()
	sitLookAnim:Stop()
	aggrovateStartAnim:Stop()
	aggrovatingAnim:Stop()
	aggrovateEndAnim:Stop()
	claw1Anim:Stop()
	claw2Anim:Stop()
	dashAnim:Stop()
	dashGrabAnim:Stop()
	screamAnim:Stop()
	throwGrabAnim:Stop()
	throwAnim:Stop()
	shriekAnim:Stop()
	slamAnim:Stop()

	if fade then
		anim:Play(fade)
	else
		anim:Play()
	end
end


local currentTarget = nil

local function jumpRandom()
	myHuman:MoveTo(myRoot.Position + Vector3.new(math.random(-100,100)/10,0,math.random(-100,100)/10))
	myHuman.MoveToFinished:Wait(2)
end

local walkrandomCD = false

local StandardPathfindSettings = {
	AgentRadius = 2.5,
	AgentHeight = 3,
	AgentCanJump = false,
	AgentCanClimb = true,
	WaypointSpacing = 2,
	AgentMaxSlope = myHuman.MaxSlopeAngle,
	Costs = {
		Water = 20
	}
}

local cornerPos = myRoot.Position

local cornerPart = Instance.new("Part")
cornerPart.Name = "cornerPart"
cornerPart.Shape = Enum.PartType.Ball
cornerPart.Position = cornerPos
cornerPart.Color = Color3.new(1, 0, 0)
cornerPart.Transparency = 1
cornerPart.Material = Enum.Material.Neon
cornerPart.Size = Vector3.new(2,2,2)
cornerPart.Anchored = true
cornerPart.CanCollide = false
cornerPart.Parent = workspace.fx


local function playSound(spd, soundObject: Sound, parent)
	local sound = soundObject:Clone()
	sound.Parent = parent
	if spd == "random" then
		sound.PlaybackSpeed += math.random(-10,10)/100
	else
		sound.PlaybackSpeed = spd
	end
	sound:Play()
	debris:AddItem(sound, sound.TimeLength)

	return sound
end
local function emitParticle(emitamt,particle1:ParticleEmitter,parent,attachment)
	local particle = particle1:Clone()
	particle.Parent = parent
	if attachment then
		local a = Instance.new("Attachment")
		a.Parent = parent
		particle.Parent = a
		debris:AddItem(a, particle.Lifetime.Max)
	end
	if particle then
		task.wait(0.03)
		particle:Emit(emitamt)
		debris:AddItem(particle, particle.Lifetime.Max)
		return particle
	end
end

local pathAmt = 0

local currentPathTarget = nil

function findPath(target)
	if target then
		local fsuccess = true
		if currentPathTarget == target then
			if currentPathTarget or target then
				fsuccess = false
			end
		end
		if fsuccess then
			currentPathTarget = target
			pathAmt += 1
			local pathAmtStart = pathAmt

			local startpos = target.Position

			local path: Path = game:GetService("PathfindingService"):CreatePath(StandardPathfindSettings)
			if target then
				local sc, err = pcall(function()
					path:ComputeAsync(myRoot.Position,target.Position)
				end)
				if sc then
					local waypoints = path:GetWaypoints()

					local blocked = false
					local c
					c = path.Blocked:Connect(function()
						blocked = true
						c:Disconnect()
					end)

					local function checkStillTarget()
						local success = nil
						if currentTarget then
							if currentTarget == target then
								local thum = target.Parent:FindFirstChildWhichIsA("Humanoid")
								if thum then
									if thum.Health > 0 then
										success = true
									end
								else
									success = true
								end
							end
						end
						if pathAmt ~= pathAmtStart then
							success = false
						end
						if not success then
							if currentPathTarget == target then
								currentPathTarget = nil
							end
						end
						return success
					end		

					local complete = false

					if path.Status == Enum.PathStatus.Success then

						task.spawn(function()
							for i, waypoint:PathWaypoint in ipairs(waypoints) do
								if i ~= 1 then
									if waypoint then
										if pathAmt == pathAmtStart then
											if not checkStillTarget() then return end

											if blocked then
												jumpRandom()
												currentPathTarget = nil
												findPath(target)
												break
											end

											myHuman:MoveTo(waypoint.Position)

											local timeOut = "doing"
											task.spawn(function()
												local a = myHuman.MoveToFinished:Wait(2)
												timeOut = a
											end)
											repeat task.wait() until timeOut ~= "doing" or (target.Position - startpos).magnitude > 2.5 or (myRoot.Position - waypoint.Position).magnitude <= 4.5
											if not checkStillTarget() then return end

											if not timeOut then
												jumpRandom()
												currentPathTarget = nil
												findPath(target)
												break
											end
											if i == #waypoints then
												complete = true
											end
											if (target.Position - startpos).magnitude > 2.5 then
												currentPathTarget = nil

												findPath(target)
												break
											end
											if (myRoot.Position - waypoint.Position).magnitude > 7 then
												currentPathTarget = nil
												break
											end
										else
											break
										end
									end
								end
							end
							if complete then
								if currentPathTarget == target then
									currentPathTarget = nil
								end
							end
						end)
					else
						jumpRandom()
						if currentPathTarget == target then
							currentPathTarget = nil
						end
						if currentTarget == target then
							currentTarget = nil
						end
						findPath(target)
					end
					if c then
						c:Disconnect()
					end
				else
					jumpRandom()
					if currentPathTarget == target then
						currentPathTarget = nil
					end
					if currentTarget == target then
						currentTarget = nil
					end
				end
			else
				jumpRandom()
				if currentPathTarget == target then
					currentPathTarget = nil
				end
				if currentTarget == target then
					currentTarget = nil
				end
			end
		end
	end
end

function findTarget(blacklist)
	local dist = math.huge
	local target = nil
	local potentialTargets = {}
	local seeTargets = {}

	local function check(v)
		local human = v:FindFirstChildWhichIsA("Humanoid")
		local torso = nil
		if human then
			if human.RootPart then
				torso = human.RootPart
			end
		end
		if human and torso then
			if v ~= script.Parent then
				if human.Health > 0 then
					local b = true
					if blacklist then
						if table.find(blacklist, human) then
							b = false
						end
					end
					if b then
						table.insert(potentialTargets,torso)
					end
				end
			end
		end
	end
	for i,v in ipairs(workspace:GetChildren()) do
		check(v)
	end
	for i,v in ipairs(workspace.npcs:GetChildren()) do
		check(v)
	end
	for i,v in ipairs(workspace.bosses:GetChildren()) do
		check(v)
	end
	if #potentialTargets > 0 then
		for i,v in ipairs(potentialTargets) do
			if v and v.Parent then
				table.insert(seeTargets, v)
			end
		end
	end
	if #seeTargets > 0 then
		for i,v in ipairs(seeTargets) do
			if v and v.Parent then
				if (myRoot.Position - v.Position).magnitude < dist then
					target = v
					dist = (myRoot.Position - v.Position).magnitude
				end
			end
		end
	end
	currentTarget = target
	return target
end

------------------------
local function push(strength, direction, lifetime, Yforce)
	local bv = Instance.new("BodyVelocity")
	bv.MaxForce = Vector3.new(math.huge,Yforce,math.huge)
	if strength > 200 then
		strength = 200
	end
	if strength < -200 then
		strength = -200
	end
	bv.Velocity = direction*strength
	bv.Parent = myRoot
	debris:AddItem(bv, lifetime)
	return bv
end

local function enableRun(enab)
	if enab then
		if not isRunning then
			if not PHASE2ACTIVE then
				humanoid.running.Value = true
			end
			hmrp.chase:Play()
			isRunning = true
			walkspeed.Value += runSpeed
		end
	else
		if isRunning then
			humanoid.running.Value = false
			hmrp.chase:Stop()
			isRunning = false
			walkspeed.Value -= runSpeed
		end
	end
end
enableRun(false)


local lastClaw = 1
local function clawAttack()
	task.spawn(function()
		attackCooldown += 1

		task.wait(0.25)

		if lastClaw == 1 then
			playAnim(claw1Anim)
		elseif lastClaw == 2 then
			playAnim(claw2Anim)
		end
		lastClaw +=1
		if lastClaw > 2 then
			lastClaw = 1
		end

		playSound("random", script.swipe, hmrp)
		playSound("random", script.swipeClaw, hmrp)

		local hitboxTime = 0.37

		task.wait(0.3)
		push(25, hmrp.CFrame.LookVector, 0.15, 0)
		task.wait(0.05)

		local hitbox = Instance.new("Part")
		hitbox.BrickColor = BrickColor.new("Really red")
		hitbox.Transparency = 1
		hitbox.Massless = true
		hitbox.Material = "Neon"
		hitbox.CanCollide = false
		hitbox.Anchored = false
		hitbox.Size = Vector3.new(6.75,9,7.5)
		hitbox.CFrame = hmrp.CFrame * CFrame.new(0,0,-4.5)
		hitbox.Parent = workspace.fx

		local weld = Instance.new("WeldConstraint")
		weld.Parent = hitbox
		weld.Part0 = hitbox		
		weld.Part1 = hmrp

		debris:AddItem(hitbox, hitboxTime)

		local hitChars = {}

		local hitboxEvent = hitboxModule.newHitbox(hitbox, 10, nil, humanoid, {}, false)
		local ev
		local de
		ev = hitboxEvent.Event:Connect(function(vHumanoid: Humanoid)
			if vHumanoid.Health > 0 then
				if not table.find(hitChars, vHumanoid) then
					table.insert(hitChars, vHumanoid)
					local dmg = dmgEvent:Invoke(nil, humanoid, vHumanoid, clawDMG, {}, 0)
					if dmg then
						kbEvent:Fire(vHumanoid.RootPart, 35, hmrp.CFrame.LookVector, 0.05)
						local event: BindableEvent = vHumanoid.Parent:FindFirstChild("ragdoll/stun")
						if event then
							event:Fire("s", 0.1, vHumanoid.Parent, 5)
						end
					end
					playSound("random", script.clawHit, vHumanoid.RootPart)
				end
			end
		end)

		de = hitbox.Destroying:Connect(function()
			ev:Disconnect()
			de:Disconnect()
		end)
		task.wait(0.4)
		attackCooldown -= 1
		return
	end)
end

local throwCooldown = 0
local function throwAttack()
	if throwCooldown <= 0 then
		task.spawn(function()
			attackCooldown += 1
			throwCooldown += 1
			
			playAnim(throwGrabAnim, 0.2)
			walkspeed.Value -= 10000
			jumppower.Value -= 10000
			task.wait(0.4)
			playSound(0.5, script.swipe, hmrp)
			task.wait(0.1)
			
			
			local hit = false
			
			local hitbox = Instance.new("Part")
			hitbox.BrickColor = BrickColor.new("Really red")
			hitbox.Transparency = 1
			hitbox.Massless = true
			hitbox.Material = "Neon"
			hitbox.CanCollide = false
			hitbox.Anchored = false
			hitbox.Size = Vector3.new(7,7,9)
			hitbox.CFrame = hmrp.CFrame * CFrame.new(0,0,-4.5)
			hitbox.Parent = workspace.fx

			local weld = Instance.new("WeldConstraint")
			weld.Parent = hitbox
			weld.Part0 = hitbox		
			weld.Part1 = hmrp

			debris:AddItem(hitbox, 0.25)

			local hitChars = {}

			local hitboxEvent = hitboxModule.newHitbox(hitbox, 20, nil, humanoid, {}, false)
			hitboxEvent.Event:Connect(function(vHumanoid: Humanoid)
				if not hit  then
					if vHumanoid.Health > 0 then
						if not table.find(hitChars, vHumanoid) then
							table.insert(hitChars, vHumanoid)
							local dmg = dmgEvent:Invoke(nil, humanoid, vHumanoid, 5, {}, 0)
							if dmg then
								hit = vHumanoid
								playSound("random", script.grabhitthing, vHumanoid.RootPart)
								playAnim(throwAnim, 0.15)
								local closest = findTarget({vHumanoid})
								if closest then
									hmrp.CFrame = CFrame.lookAt(hmrp.Position, Vector3.new(closest.Position.X, hmrp.Position.Y, closest.Position.Z))
								end
								
								local rconnection
								local ragdoll = vHumanoid.Parent:FindFirstChild("ragdolled")
								if ragdoll then
									rconnection = ragdoll.Changed:Connect(function()
										ragdoll.Value = true
									end)
									ragdoll.Value = true
								end
								
								local timeUntil = 0.68
								local arm = boss["Right Arm"]
								local leg = vHumanoid.RootPart

								local legGrip = boss["Right Arm"].grip
								local getGrip = Instance.new("Attachment")
								getGrip.CFrame = CFrame.new(1,0,0)
								getGrip.Parent = leg
								legGrip.Parent = arm
								
								local conenctions = {}

								local alignPos = Instance.new("AlignPosition")
								alignPos.RigidityEnabled = true
								alignPos.Responsiveness = 100
								alignPos.Attachment0 = getGrip
								alignPos.Attachment1 = legGrip
								alignPos.Parent = leg
								local alignRot = Instance.new("AlignOrientation")
								alignRot.RigidityEnabled = true
								alignRot.Responsiveness = 100
								alignRot.Attachment0 = getGrip
								alignRot.Attachment1 = legGrip
								alignRot.Parent = leg
								debris:AddItem(alignPos, timeUntil-0.02)
								debris:AddItem(getGrip, timeUntil-0.01)
								debris:AddItem(alignRot, timeUntil-0.02)
								task.wait(0.58)
								playSound("random", script.swipe, hmrp)
								task.wait(0.1)
								if vHumanoid then
									local hitbox = Instance.new("Part")
									hitbox.BrickColor = BrickColor.new("Really red")
									hitbox.Transparency = 1
									hitbox.Massless = true
									hitbox.Shape = Enum.PartType.Ball
									hitbox.Material = "Neon"
									hitbox.CanCollide = false
									hitbox.Anchored = false
									hitbox.Size = Vector3.new(7,7,7)
									hitbox.CFrame = vHumanoid.RootPart.CFrame
									hitbox.Parent = workspace.fx

									local weld = Instance.new("Weld")
									weld.Parent = hitbox
									weld.Part0 = hitbox
									weld.Part1 = vHumanoid.RootPart
									if rconnection then
										rconnection:Disconnect()
									end
									if ragdoll then
										ragdoll.Value = false
									end
									local event: BindableEvent = vHumanoid.Parent:FindFirstChild("ragdoll/stun")
									if event then
										event:Fire("r", 1, vHumanoid.Parent, 10)
									end
									local rchanged
									if ragdoll then
										rchanged = ragdoll.Changed:Connect(function()
											if ragdoll.Value == false then
												rchanged:Disconnect()
												hitbox:Destroy()
												hitbox = nil
												for i, v in pairs(conenctions) do
													if v then
														v:Disconnect()
													end
												end
											end
										end)
									end
									local strength = 80
									if PHASE2ACTIVE then
										strength = 95
									end
									local direction = hmrp.CFrame.LookVector+Vector3.new(0,0.1,0)
									if closest and closest.Parent then
										local pos = closest.Position
										if math.random(1,3) == 1 then
											pos = solvePrediction(strength, closest.Position, closest.AssemblyLinearVelocity, hitbox.Position)
										end
										direction = CFrame.lookAt(hitbox.Position, pos).LookVector
									end
									local bv = Instance.new("BodyVelocity")
									bv.Velocity = direction*strength
									bv.MaxForce = Vector3.new(math.huge,math.huge,math.huge)
									bv.Parent = vHumanoid.RootPart
									debris:AddItem(bv, 0.2)
									
									local hitChars = {}

									
									hitbox.Touched:Connect(function(hit)
										if hit and hit.Parent then
											if math.abs(hitbox.AssemblyLinearVelocity.Magnitude) > 7 then
												local vHumanoid = hit.Parent:FindFirstChildWhichIsA("Humanoid")
												if vHumanoid then
													if vHumanoid ~= humanoid then
														if not table.find(hitChars, vHumanoid) then
															table.insert(hitChars, vHumanoid)
															local dmg = dmgEvent:Invoke(nil, humanoid, vHumanoid, 35, {}, 0)
															if dmg then
																if hitbox then
																	kbEvent:Fire(vHumanoid.RootPart, 50, CFrame.lookAt(Vector3.zero, hitbox.AssemblyLinearVelocity).LookVector, 0.15)
																	playSound("random", script.throwhit, vHumanoid.RootPart)

																	local event: BindableEvent = vHumanoid.Parent:FindFirstChild("ragdoll/stun")
																	if event then
																		event:Fire("r", 0.65, vHumanoid.Parent, 0)
																	end
																end
															end
														end
													end
												end
											end
										end
									end)
									local function check(y, p)
										local success = false
										if p and p.Parent then
											if p.Position.y > y+0.15 or p.Position.y < y-0.15 then
												success = true
											end
										else
											success = true
										end
										return success
									end
									local dmgcooldown = false
									local firstdmg = 38.25
									
									local touchcd = false
			
									for i, p in pairs(vHumanoid.Parent:GetChildren()) do
										if p:IsA("BasePart") then
											local connection
											connection = p.Touched:Connect(function(hit)
												if ragdoll.Value == true then
													if hit.CanCollide == true then
														if hit.CollisionGroup ~= "plrs" then
															if not hit.Parent:FindFirstChildWhichIsA("Humanoid") then
																if not touchcd then
																	touchcd = true
																	if not dmgcooldown then
																		local dmg = dmgEvent:Invoke(nil, humanoid, vHumanoid, firstdmg, {}, 99999)
																		if dmg then
																			dmgcooldown = true
																			

																			local event: BindableEvent = vHumanoid.Parent:FindFirstChild("ragdoll/stun")
																			if event then
																				event:Fire("s", 0.75, vHumanoid.Parent, 0)
																			end
																			firstdmg /= 2
																			task.spawn(function()
																				task.wait(0.5)
																				dmgcooldown = false
																			end)
																		end
																	end
																	playSound(math.random(0.75*100,1.35*100)/100, script.throwhitwall, p)
																	local y = p.Position.Y
																	repeat task.wait() until check(y, p)
																	task.wait(0.1)
																	touchcd = false
																end
															end
														end
													end
												else
													connection:Disconnect()
												end
											end)
											table.insert(conenctions, connection)
										end
									end
									
								end
								task.wait(0.75)
								walkspeed.Value += 10000
								jumppower.Value += 10000
								attackCooldown -= 1
								task.wait(3)
								throwCooldown -= 1
							end
						end
					end
				end
			end)
			task.wait(0.5)
			if not hit then
				walkspeed.Value += 10000
				jumppower.Value += 10000
				attackCooldown -= 1
				task.wait(3)
				throwCooldown -= 1
			end
		end)
	end
end

local function dashAttack()
	task.spawn(function()
		attackCooldown += 1
		
		local s = 85
		if PHASE2ACTIVE then
			s = 100
		end
		
		if currentTarget then
			local pos = currentTarget.Position
			if math.random(1,3) == 1 then
				local vel = currentTarget.AssemblyLinearVelocity
				local num = 0.75
				local firstpos = currentTarget.Position + (vel*num)
				pos = solvePrediction(s, firstpos, vel, hmrp.Position)
				local param = RaycastParams.new()
				param.FilterType = Enum.RaycastFilterType.Exclude
				param.FilterDescendantsInstances = filter2
				local ray = workspace:Raycast(currentTarget.Position, CFrame.lookAt(currentTarget.Position, pos).LookVector*(currentTarget.Position-pos).Magnitude, param)
				if ray then
					pos = ray.Position
				end
			end
			hmrp.CFrame = CFrame.lookAt(hmrp.Position, Vector3.new(pos.X, hmrp.Position.Y, pos.Z))
		end

		task.wait(0.2)

		walkspeed.Value -= 1000
		jumppower.Value -= 1000

		humanoid.AutoRotate = false

		playAnim(dashAnim)
		push(-38, hmrp.CFrame.LookVector, 0.2, 0)


		local hitboxTime = 0.75

		task.spawn(function()
			task.wait(0.65)
			playSound("random", script.swipe, hmrp)
			push(s, hmrp.CFrame.LookVector+Vector3.new(0,0.2,0), 0.25, math.huge)
			return
		end)

		task.wait(0.47)

		local hitbox = Instance.new("Part")
		hitbox.BrickColor = BrickColor.new("Really red")
		hitbox.Transparency = 1
		hitbox.Massless = true
		hitbox.Material = "Neon"
		hitbox.CanCollide = false
		hitbox.Anchored = false
		hitbox.Size = Vector3.new(8,13,8)
		hitbox.CFrame = hmrp.CFrame * CFrame.new(0,0,-4.5)
		hitbox.Parent = workspace.fx

		local weld = Instance.new("WeldConstraint")
		weld.Parent = hitbox
		weld.Part0 = hitbox		
		weld.Part1 = hmrp

		debris:AddItem(hitbox, hitboxTime)

		local success = false

		local touchConnection

		local cantHit = {}

		local hitboxEvent = hitboxModule.newHitbox(hitbox, 10, nil, humanoid, {}, false)
		local de
		touchConnection = hitboxEvent.Event:Connect(function(vHumanoid: Humanoid)
			if vHumanoid.Health > 0 then
				if not table.find(cantHit, vHumanoid) then
					local check = dmgEvent:Invoke(nil, humanoid, vHumanoid, 0, {}, 1)

					table.insert(cantHit, vHumanoid)

					if check then
						touchConnection:Disconnect()
						de:Disconnect()
						success = true

						hitbox:Destroy()
						hitbox = nil
						local dmg = dmgEvent:Invoke(nil, humanoid, vHumanoid, 7, {}, 100)

						if dmg then
							playAnim(dashGrabAnim)
							playSound(1, script.kill, vHumanoid.RootPart)

							local stunValue: BoolValue = vHumanoid.Parent:FindFirstChild("ragdolled")
							stunValue.Value = true

							local ragdollBackConnection
							ragdollBackConnection = stunValue.Changed:Connect(function()
								stunValue.Value = true
							end)

							local timeUntil = 2.27
							local endUntil = dashGrabAnim.Length

							local arm = boss["Right Arm"]
							local leg = vHumanoid.RootPart

							local legGrip = boss["Right Arm"].grip
							local getGrip = Instance.new("Attachment")
							getGrip.Parent = leg
							legGrip.Parent = arm

							local alignPos = Instance.new("AlignPosition")
							alignPos.Parent = leg
							alignPos.RigidityEnabled = true
							alignPos.Responsiveness = 1000
							alignPos.Attachment0 = getGrip
							alignPos.Attachment1 = legGrip
							debris:AddItem(alignPos, timeUntil)
							debris:AddItem(getGrip, timeUntil)

							task.wait(1.25)
							dmgEvent:Invoke(nil, humanoid, vHumanoid, 12, {}, 100)
							task.wait((endUntil-timeUntil)-1.15)
							dmgEvent:Invoke(nil, humanoid, vHumanoid, 75, {}, 100)

							task.wait(endUntil-(endUntil-timeUntil)-1.15)							
							if ragdollBackConnection then
								ragdollBackConnection:Disconnect()
								if vHumanoid then
									if stunValue then
										stunValue.Value = false
									end
									local event: BindableEvent = vHumanoid.Parent:FindFirstChild("ragdoll/stun")
									if event then
										event:Fire("r", 0.5, vHumanoid.Parent, 10)
									end
								end
							end
						end
						walkspeed.Value += 1000
						jumppower.Value += 1000
						humanoid.AutoRotate = true
						attackCooldown -= 1
					end
				end
			end
		end)

		de = hitbox.Destroying:Connect(function()
			if touchConnection then
				touchConnection:Disconnect()
			end
			de:Disconnect()
		end)
		task.wait(2.5)
		if not success then
			walkspeed.Value += 1000
			jumppower.Value += 1000
			humanoid.AutoRotate = true
		end
		task.wait(1)
		if not success then
			attackCooldown -= 1
		end
		return
	end)
end

local function screamAttack()
	attackCooldown += 1
	screamCooldown += 1

	walkspeed.Value -= 1000
	jumppower.Value -= 1000

	hmrp.scream:Play()

	playAnim(screamAnim)
	hmrp.RootAttachment.star:Emit(1)



	local hitboxTime = 0.45
	local hitboxSize = 135

	enableRun(false)

	task.wait(2.2)

	hmrp.scream:Stop()

	playSound(1.1, script.screamSFX, hmrp)

	hmrp.RootAttachment.burst:Emit(1)
	hmrp.RootAttachment.scream:Emit(5)

	local hitbox = Instance.new("Part")
	hitbox.BrickColor = BrickColor.new("Really red")
	hitbox.Transparency = 1
	hitbox.Massless = true
	hitbox.Material = "Neon"
	hitbox.CanCollide = false
	hitbox.Anchored = true
	hitbox.Shape = Enum.PartType.Ball
	hitbox.Size = Vector3.new(hitboxSize,hitboxSize,hitboxSize)
	hitbox.CFrame = hmrp.CFrame
	hitbox.Parent = workspace.fx

	debris:AddItem(hitbox, hitboxTime)

	local hitChars = {}
	local function onHit(vHumanoid: Humanoid)
		if vHumanoid.Health > 0 then
			if not table.find(hitChars, vHumanoid) then
				table.insert(hitChars, vHumanoid)

				local rayParam = RaycastParams.new()
				rayParam.FilterType = Enum.RaycastFilterType.Exclude
				rayParam.FilterDescendantsInstances = filter
				local raycast = workspace:Raycast(hitbox.Position, CFrame.lookAt(hitbox.Position, vHumanoid.RootPart.Position).LookVector*1000, rayParam)

				local SUCCESS = false

				if raycast then
					if raycast.Instance.Parent then
						if raycast.Instance.Parent == vHumanoid.Parent then
							SUCCESS = true
						end
					end
				end

				if SUCCESS then
					local dmg = dmgEvent:Invoke(nil, humanoid, vHumanoid, 80, {}, 1)
					if dmg then
						local plr = game:GetService("Players"):GetPlayerFromCharacter(vHumanoid.Parent)
						if plr then
							game:GetService("ReplicatedStorage").remoteEvents.playClientSound:FireClient(plr, script.ringing)
						end
						kbEvent:Fire(vHumanoid.RootPart, 55, CFrame.lookAt(hitbox.Position-Vector3.new(0,1.5,0), vHumanoid.RootPart.Position).LookVector, 0.09)
						local event: BindableEvent = vHumanoid.Parent:FindFirstChild("ragdoll/stun")
						if event then
							event:Fire("s", 0.5, vHumanoid.Parent, 12)
							event:Fire("r", 0.5, vHumanoid.Parent, 5)
						end
					end
				else
					local find = table.find(hitChars, vHumanoid)
					if find then
						table.remove(hitChars, find)
					end
				end
			end
		end
	end	
	local hitboxEvent = hitboxModule.newHitbox(hitbox, 75, nil, humanoid, {}, false)
	hitboxEvent.Event:Connect(function(v)
		onHit(v)
	end)

	task.wait(1.5)
	attackCooldown -= 1

	walkspeed.Value += 1000
	jumppower.Value += 1000

	task.spawn(function()
		task.wait(5)
		screamCooldown -= 1
		return
	end)
end

local shriekCooldown = 0
local function shriekAttack()
	if shriekCooldown <= 0 then
		shriekCooldown += 1
		attackCooldown += 1
		walkspeed.Value -= 100000
		jumppower.Value -= 100000
		playAnim(shriekAnim, 0.25)
		task.wait(0.8)
		
		enableRun(false)

		local headpos = boss.Head.Position

		local speed = 50

		playSound("random", script.shriekscream, hmrp)
		local function summon()
			local pos = (hmrp.CFrame*CFrame.new(0,0,-10)).Position
			local target:BasePart = currentTarget
			if target then
				pos = target.Position
				if math.random(1,3) ~= 1 then
					local vel = target.AssemblyLinearVelocity
					pos = solvePrediction(speed, target.Position, vel, headpos)
					local param = RaycastParams.new()
					param.FilterType = Enum.RaycastFilterType.Exclude
					param.FilterDescendantsInstances = filter2
					local ray = workspace:Raycast(currentTarget.Position, CFrame.lookAt(currentTarget.Position, pos).LookVector*(currentTarget.Position-pos).Magnitude, param)
					if ray then
						pos = ray.Position
					end
				end
			end
			local model = game:GetService("ReplicatedStorage").BOSSASSETS.shriekprojectile:Clone()
			local hitbox = model.hitbox
			hitbox.CFrame = CFrame.lookAt(headpos, pos)
			local bv = Instance.new("BodyVelocity")
			bv.MaxForce = Vector3.new(math.huge,math.huge,math.huge)
			bv.Velocity = hitbox.CFrame.LookVector*speed
			bv.Parent = hitbox
			model.Parent = workspace.fx
			hitbox:SetNetworkOwner(nil)

			local lifetime = 5
			ts:Create(model.scale, TweenInfo.new(lifetime, Enum.EasingStyle.Linear, Enum.EasingDirection.Out), {Value = 4}):Play()
			debris:AddItem(model, lifetime)

			local hitChars = {}

			hitbox.Touched:Connect(function(hit)
				if hitbox and hitbox.Parent then
					if hit and hit.Parent then
						local vHumanoid = hit.Parent:FindFirstChildWhichIsA("Humanoid")
						if vHumanoid then
							if vHumanoid ~= humanoid then
								if vHumanoid.Health > 0 then
									if not table.find(hitChars, vHumanoid) then
										table.insert(hitChars, vHumanoid)
										local dmg = dmgEvent:Invoke(nil, humanoid, vHumanoid, 41*model:GetScale(), {}, 1)
										if dmg then
											if hitbox then
												kbEvent:Fire(vHumanoid.RootPart, 35, hitbox.CFrame.LookVector, 0.15)
												playSound(0.65, script.throwhitwallhit, vHumanoid.RootPart)

												local plr = game:GetService("Players"):GetPlayerFromCharacter(vHumanoid.Parent)
												if plr then
													game:GetService("ReplicatedStorage").remoteEvents.playClientSound:FireClient(plr, script.ringing)
												end

												local event: BindableEvent = vHumanoid.Parent:FindFirstChild("ragdoll/stun")
												if event then
													event:Fire("s", 0.95*model:GetScale(), vHumanoid.Parent, 0)
												end
												emitParticle(3, script.shriekhitparticle, vHumanoid.RootPart, true)
											end
										end
									end
								end
							end
						end
					end
				end
			end)
		end
		
		summon()
		if PHASE2ACTIVE then
			for i=1,2 do
				task.wait(0)
				summon()
			end
		end


		task.wait(0.88)
		attackCooldown -= 1
		walkspeed.Value += 100000
		jumppower.Value += 100000
		task.spawn(function()
			task.wait(3)
			shriekCooldown -= 1
			return
		end)
	end
end


local slamCooldown = 0
local function slamAttack()
	slamCooldown+=1
	attackCooldown += 1
	walkspeed.Value -= 100000
	jumppower.Value -= 100000
	if currentTarget then
		hmrp.CFrame = CFrame.lookAt(hmrp.Position, Vector3.new(currentTarget.Position.X, hmrp.Position.Y, currentTarget.Position.Z))
	end
	hmrp.RootAttachment.star:Emit(1)
	playAnim(slamAnim, 0.25)
	enableRun(false)
	playSound("random", script.yell, hmrp)
	task.wait(0.8)

	local hitbox = Instance.new("Part")
	hitbox.BrickColor = BrickColor.new("Really red")
	hitbox.Transparency = 1
	hitbox.Massless = true
	hitbox.Material = "Neon"
	hitbox.CanCollide = false
	hitbox.Anchored = false
	hitbox.Size = Vector3.new(8,13,8)
	hitbox.CFrame = hmrp.CFrame * CFrame.new(0,0,-4.5)
	hitbox.Parent = workspace.fx

	local weld = Instance.new("WeldConstraint")
	weld.Parent = hitbox
	weld.Part0 = hitbox		
	weld.Part1 = hmrp

	debris:AddItem(hitbox, 0.25)

	local hitChars = {}

	local hitboxEvent = hitboxModule.newHitbox(hitbox, 25, nil, humanoid, {}, false)
	hitboxEvent.Event:Connect(function(vHumanoid: Humanoid)
		if vHumanoid.Health > 0 then
			if not table.find(hitChars, vHumanoid) then
				table.insert(hitChars, vHumanoid)
				local dmg = dmgEvent:Invoke(nil, humanoid, vHumanoid, 70, {}, 0)
				if dmg then
					kbEvent:Fire(vHumanoid.RootPart, -55, hmrp.CFrame.UpVector+Vector3.new(0,0.1,0), 0.05)
					local event: BindableEvent = vHumanoid.Parent:FindFirstChild("ragdoll/stun")
					if event then
						event:Fire("s", 2, vHumanoid.Parent, 5)
						event:Fire("r", 0.75, vHumanoid.Parent, 5)
					end
					playSound("random", script.slamhit, vHumanoid.RootPart)
				end
			end
		end
	end)

	task.wait(0.025)
	playSound("random", script.slamSFX, hmrp)
	task.wait(0.075)

	for i, v in pairs(hmrp.windparticles:GetChildren()) do
		v:Emit(5)
	end

	local hitbox = Instance.new("Part")
	hitbox.BrickColor = BrickColor.new("Really red")
	hitbox.Transparency = 1
	hitbox.Massless = true
	hitbox.Material = "Neon"
	hitbox.CanCollide = false
	hitbox.Anchored = true
	hitbox.Shape = Enum.PartType.Ball
	hitbox.Size = Vector3.new(25,25,25)
	hitbox.CFrame = hmrp.CFrame
	hitbox.Parent = workspace.fx

	debris:AddItem(hitbox, 0.25)

	local hitChars = {}

	local hitboxEvent = hitboxModule.newHitbox(hitbox, 25, nil, humanoid, {}, false)
	hitboxEvent.Event:Connect(function(vHumanoid: Humanoid)
		if vHumanoid.Health > 0 then
			if not table.find(hitChars, vHumanoid) then
				table.insert(hitChars, vHumanoid)
				local dmg = dmgEvent:Invoke(nil, humanoid, vHumanoid, 70, {}, 0)
				if dmg then
					kbEvent:Fire(vHumanoid.RootPart, 65, CFrame.lookAt(hitbox.Position+Vector3.new(0,2.5,0),vHumanoid.RootPart.Position).LookVector, 0.05)
					local event: BindableEvent = vHumanoid.Parent:FindFirstChild("ragdoll/stun")
					if event then
						event:Fire("s", 1, vHumanoid.Parent, 10)
						event:Fire("r", 0.5, vHumanoid.Parent, 10)
					end
				end
			end
		end
	end)

	local map = workspace.currentMap:GetChildren()[1]
	if map then
		if map.Name == "096" then
			map.MAP.tileFloor.rocks:Emit(555)

			local hitbox:BasePart = map.MAP.tileFloorTouching:Clone()
			hitbox.Parent = workspace.fx
			debris:AddItem(hitbox, 0.35)

			local hitChars = {}
			local hitboxEvent = hitboxModule.newHitbox(hitbox, 25, nil, humanoid, {["dead"] = false}, false)
			hitboxEvent.Event:Connect(function(vHumanoid: Humanoid)
				if vHumanoid ~= humanoid then
					if not table.find(hitChars, vHumanoid) then
						table.insert(hitChars, vHumanoid)
						local dmg = dmgEvent:Invoke(nil, humanoid, vHumanoid, 51.5, {}, 1)
						if dmg then
							kbEvent:Fire(vHumanoid.RootPart, math.random(30*100,70*100)/100, Vector3.new(0,1,0), 0.15)

							local event: BindableEvent = vHumanoid.Parent:FindFirstChild("ragdoll/stun")
							if event then
								event:Fire("s", 0.65, vHumanoid.Parent, 5)
							end
						end
					end
				end
			end)
		end
	end

	task.wait(1.4)
	enableRun(true)
	attackCooldown -= 1
	walkspeed.Value += 100000
	jumppower.Value += 100000
	task.spawn(function()
		task.wait(math.random(5*100,10*100)/100)
		slamCooldown -=1
	end)
end

-----------------------

function died()
	game:GetService("Debris"):AddItem(script.Parent,5)
	for i, v in pairs(hmrp:GetChildren()) do
		if v:IsA("Sound") then
			if v.IsPlaying then
				v:Stop()
				v.Volume = 0
			end
		end
	end
end

myHuman.Died:Connect(died)

local function isBehind(CharHRP,Part)
	local point1,point2 = (CharHRP.CFrame + CharHRP.CFrame.LookVector),(CharHRP.CFrame + CharHRP.CFrame.LookVector*-1)
	local mag1,mag2 = (point1.Position-Part.Position).Magnitude,(point2.Position-Part.Position).Magnitude
	return not (mag1 <= mag2)
end

local phase2stopcdthing = false
function main()
	if currentTarget and currentTarget ~= cornerPart and phase2stopcdthing == false then
		local target = currentTarget
		target = target.Parent
		local tHmrp = target.PrimaryPart
		if tHmrp then
			aggrovated = true
			enableRun(true)

			local distance = (hmrp.Position - tHmrp.Position).Magnitude

			findPath(target:FindFirstChild("HumanoidRootPart"))

			if attackCooldown <= 0 then
				if not (tHmrp.Position.Y > hmrp.Position.Y+5.5) then
					if distance <= 13 then
						local doDash = math.random(1,4)
						if doDash == 1 then
							dashAttack()
						else
							local doScream = math.random(1,6)
							if doScream == 1 and screamCooldown <= 0 then
								screamAttack()
							else
								if math.random(1,3) ~= 1 and shriekCooldown <= 0 and PHASE2ACTIVE then
									slamAttack()
								else
									if math.random(1,2) == 1 and throwCooldown <= 0 then
										throwAttack()
									else
										clawAttack()
									end
								end
							end
						end
					elseif distance > 13 and distance < 34 then
						local doScream = math.random(1,4)
						if doScream == 1 and screamCooldown <= 0 then
							screamAttack()
						else
							if math.random(1,6) == 1 and slamCooldown <= 0 and PHASE2ACTIVE then
								slamAttack()
							else
								if math.random(1,3) ~= 1 and shriekCooldown <= 0 then
									shriekAttack()
								else
									if math.random(1,3) ~= 1 then
										dashAttack()
									end
								end
							end
						end
					elseif distance >= 34 and distance < 65 then
						local doScream = math.random(1,10)
						if doScream == 1 and screamCooldown <= 0 then
							screamAttack()
						else
							if math.random(1,7) == 1 and slamCooldown <= 0 and PHASE2ACTIVE then
								for i=1,3 do
									slamAttack()
								end
								shriekAttack()
							end
						end
					end
				else
					if math.random(1,5) == 1 and shriekCooldown <= 0 then
						shriekAttack()
					end
				end
			end
		end
		if humanoid:GetState() == Enum.HumanoidStateType.Ragdoll or humanoid:GetState() == Enum.HumanoidStateType.FallingDown then
			npcEvent:FireAllClients(humanoid, Enum.HumanoidStateType.Running, true, "change")
			humanoid:ChangeState(Enum.HumanoidStateType.Running)
		end
	else
		if not PHASE2ACTIVE then
			aggrovated = false

			findPath(currentTarget)

			enableRun(false)

			task.spawn(function()
				if not isCorner then
					isCorner = true
					local minus = walkspeed.Value
					walkspeed.Value -= minus

					hmrp.cry:Play()

					playAnim(sitAnim, 1.25)

					repeat task.wait() until aggrovated or ((hmrp.Position - Vector3.new(cornerPos.X,hmrp.Position.Y,cornerPos.Z)).Magnitude > cornerDistance*5)

					hmrp.cry:Stop()

					if aggrovated then
						sitAnim:Stop()
					else
						sitAnim:Stop(0.5)
					end
					walkspeed.Value += minus
					isCorner = false
				end
				return
			end)
		else
			
		end
	end
end

if not myRoot.Anchored then
	myRoot:SetNetworkOwner(nil)
end

local notmoveStreak = 0

task.wait(0.07)

isCorner = true
local minus1 = walkspeed.Value
walkspeed.Value -= minus1

hmrp.cry:Play()

playAnim(sitAnim, 1.25)

immunites.DMGIMMUNE.Value+=1000

while true do
	task.wait(0.1)
	local target: Model = findTarget(nil)
	if target then
		local tHmrp = target
		if tHmrp then
			local maxDist = maxDistance	
			local tPos = tHmrp.Position
			local distance = (hmrp.Position - tPos).Magnitude
			if distance < 9 then
				hmrp.cry:Stop()

				playAnim(sitLookAnim)
				walkspeed.Value += 3
				isCorner = false

				task.wait(sitLookAnim.Length - 0.95)
				immunites.DMGIMMUNE.Value-=1000
				immunites.defense.Value += 50
				playAnim(aggrovateStartAnim)
				humanoid:MoveTo(hmrp.Position:Lerp(tPos, 0.45))
				task.wait(aggrovateStartAnim.Length-0.15)
				playAnim(aggrovatingAnim)
				hmrp.scream:Play()
				hmrp.angry:Play()
				task.wait(math.random(140,190)/10)
				immunites.defense.Value -= 50
				hmrp.scream:Stop()
				hmrp.angry:Stop()
				aggrovatingAnim:Stop()
				walkspeed.Value += minus1-3
				isCorner = false
				playAnim(aggrovateEndAnim)
				break
			end
		end
	end
end

local playingSong = game:GetService("SoundService").playingSong
if boss.domusic.Value then
	local music: Sound = script.music
	if music then
		playingSong.SoundId = music.SoundId
		playingSong.PlaybackSpeed = music.PlaybackSpeed
		playingSong.Volume = music.Volume
		playingSong:Play()

		playingSong.requiredVolume.Value = music.Volume
	end
end

local h
local r = true
h = humanoid.Changed:Connect(function()
	if r then
		if humanoid.Health < humanoid.MaxHealth/2 then
			if humanoid.Health > 0 then
				r = false
				h:Disconnect()

				PHASE2ACTIVE = true

				attackCooldown += 1
				repeat task.wait() until attackCooldown <= 1
				
				local map = workspace.currentMap:GetChildren()[1]
				
				if map and map.Name == "096" then
					task.spawn(function()
						task.wait(8)


						if map and map.Parent then
							if map.Name == "096" then
								map.MAP.alarms.toggle.Value = true

								local color = game:GetService("Lighting"):FindFirstChildWhichIsA("ColorCorrectionEffect")
								if color then
									ts:Create(color, TweenInfo.new(5), {TintColor = Color3.fromRGB(255, 144, 144)}):Play()
								end
							end
							task.wait(15)
							if humanoid and humanoid.Parent and map and map.Parent then
								if humanoid.Health > 0 then
									local team = game:GetService("Teams"):FindFirstChild("players")
									map.MAP.gate2.open.Value = true
									for i=1,math.random(3,5) do
										local guard = game:GetService("ReplicatedStorage")["096GuardHelper"]:Clone()
										guard.NPCTEAMVALUE.Value = team
										local firstpos = (CFrame.new(Vector3.new(map.roomDetect.Position.X,4.25,map.roomDetect.Position.Z))*CFrame.Angles(0,math.rad(math.random(0,360*100)/100),0))*CFrame.new(0,0,math.random(0,8*100)/100)
										guard.HumanoidRootPart.CFrame = CFrame.new(firstpos.Position)*CFrame.Angles(0,math.rad(90),0)
										guard.Parent = workspace.npcs
									end
								end
							end
						end
					end)
				end
				
				phase2stopcdthing = true
				enableRun(false)
				boss.Animate.Enabled = false
				boss.rageAnimate.Enabled = true
				
				walkspeed.Value -= 1000000
				jumppower.Value -= 1000000
				
				phase2startAnim:Play(0.35)
				
				playSound("random", script.critical, hmrp)
				playSound(1.5, script.violin, hmrp)
				task.wait(4.45)
				playSound("random", script.ragescream, hmrp)
				hmrp.RootAttachment.burst:Emit(5)
				task.wait(0.25)
				if map then
					if map.Name == "096" then
						map.MAP.tileFloor.cracks.Transparency = 0.5
						map.MAP.tileFloor.rocks:Emit(555)
						map.MAP.tileFloor.rockbreak:Play()

						local hitbox:BasePart = map.MAP.tileFloorTouching:Clone()
						hitbox.Parent = workspace.fx
						debris:AddItem(hitbox, 0.35)

						local hitChars = {}
						local hitboxEvent = hitboxModule.newHitbox(hitbox, 25, nil, humanoid, {["dead"] = false}, false)
						hitboxEvent.Event:Connect(function(vHumanoid: Humanoid)
							if vHumanoid ~= humanoid then
								if not table.find(hitChars, vHumanoid) then
									table.insert(hitChars, vHumanoid)
									local dmg = dmgEvent:Invoke(nil, humanoid, vHumanoid, 92.7, {}, 1)
									if dmg then
										kbEvent:Fire(vHumanoid.RootPart, math.random(30*100,70*100)/100, Vector3.new(0,1,0), 0.15)

										local event: BindableEvent = vHumanoid.Parent:FindFirstChild("ragdoll/stun")
										if event then
											event:Fire("r", 0.65, vHumanoid.Parent, 0)
										end
									end
								end
							end
						end)
					end
				end
				task.wait(0.75)
				if humanoid.Parent.domusic.Value == true then
					local music: Sound = script.phase2music
					if music then
						playingSong.SoundId = music.SoundId
						playingSong.PlaybackSpeed = music.PlaybackSpeed
						playingSong.Volume = music.Volume
						playingSong:Play()

						playingSong.requiredVolume.Value = music.Volume
					end
				end
				task.wait(0.95)
				script.chorus.Parent = hmrp.chase
				hmrp.chase.PlaybackSpeed = 0.9
				phase2stopcdthing = false
				enableRun(true)
				walkspeed.Value += 1000000+4
				jumppower.Value += 1000000
				attackCooldown -= 1
			end
		end
	end
end)

enableRun(true)
task.wait(1.35)
aggrovateEndAnim:Stop(0.65)

game:GetService("RunService").Heartbeat:Connect(function()
	if not myRoot.Anchored then
		if myRoot:IsDescendantOf(workspace) then
			if myRoot:GetNetworkOwner() then
				myRoot:SetNetworkOwner(nil)
			end
		end
	end
	currentTarget = findTarget(nil)
	if not currentTarget then
		if not PHASE2ACTIVE then
			currentTarget = cornerPart
		end
	end
	
	if myHuman.SeatPart then
		myHuman.Jump = true
	end
end)
while true do
	task.wait(0.25)
	if myHuman.Health <= 0 then
		break
	end
	main()
end



















any help would be greatly appreciated

2 Likes

If the boss is vanishing, I’d probably start by printing it’s target, position or any other variables involved at that time and see what’s being reported when things go wrong. I’d think chances are fair that a CFrame is being reset or using the wrong part reference but that’s just a guess at this point.

That’s nearly 2k lines of heavily nested, uncommented code. While I generally feel that many users don’t provide enough code to properly understand their issues, that is a lot of code to digest and try to come up with a suggestion.

Any chance you could add a few comments in to help understand what’s going on at various points? I find I’ll often discover my own issues while trying to explain what I’ve created.

1 Like

the thing that i’m upset is not the code, is the boss lol, it is so well worked, and cool, but i can’t seem to help what you want, maybe try checking on the workspace if he got deleted? or maybe the parts get transparent? idk

Even if it was finding a new target, it surely woudln’t dissapear. I think it’s do with some of your velocity/force approaches. Maybe you’re giving the NPC too much power. But then again I didn’t read your full code but that might be the reason. If it has too much force added to itself it might fling to the void. Or if you’re applying the velocity/force on another player besides the NPC it may be getting affected still.

1 Like

I would say that this is right, but a thing that is weird, that is happening is when he respawns, and sometimes on random times, but i still think he got destroyed or he got teleported maybe? i don’t know, but what i know is that maybe some other script is affecting this, like, this affected me on much of my games, maybe try checking if other scripts is affecting the guy.

I actually think another reason could be this:

task.spawn(function()
							for i, waypoint:PathWaypoint in ipairs(waypoints) do
								if i ~= 1 then
									if waypoint then
										if pathAmt == pathAmtStart then
											if not checkStillTarget() then return end

											if blocked then
												jumpRandom()
												currentPathTarget = nil
												findPath(target)
												break
											end

											myHuman:MoveTo(waypoint.Position)

											local timeOut = "doing"
											task.spawn(function()
												local a = myHuman.MoveToFinished:Wait(2)
												timeOut = a
											end)
											repeat task.wait() until timeOut ~= "doing" or (target.Position - startpos).magnitude > 2.5 or (myRoot.Position - waypoint.Position).magnitude <= 4.5
											if not checkStillTarget() then return end

											if not timeOut then
												jumpRandom()
												currentPathTarget = nil
												findPath(target)
												break
											end
											if i == #waypoints then
												complete = true
											end
											if (target.Position - startpos).magnitude > 2.5 then
												currentPathTarget = nil

												findPath(target)
												break
											end
											if (myRoot.Position - waypoint.Position).magnitude > 7 then
												currentPathTarget = nil
												break
											end
										else
											break
										end
									end
								end
							end

It’s unlikely, but so many task.spawns it might cause the NPC to bug out when it loses a target.

1 Like

yeah, and way too many loops can cause MUCH and much lag ngl

fixed it, not sure what exactly caused it but i edited some stuff and it seemed to stop

1 Like

ok, even if our help wasn’t succefull, have fun on your new game and your game looks neat! Hope you get famous!

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.