NPC will destroy for no reason on function when player is standing still

My NPC will always disappear and die whenever the player stands still for 1 attack. Here’s some of the required script:

Getting Closest Player

local findNearestPlayer = function(pos)
	local list = workspace:GetChildren()
	local torso = nil
	local temp = nil
	local temp2 = nil
	local human = nil

	for x = 1, #list do
		temp2 = list[x]
		
		if (temp2.className == "Model") and (temp2 ~= script.Parent) then
			temp = temp2:FindFirstChild("HumanoidRootPart")
			human = temp2:FindFirstChild("Humanoid")
			
			if (temp ~= nil) and (human ~= nil) then
				--if (temp.Position - pos).Magnitude < dist then
					local player = players:GetPlayerFromCharacter(human.Parent)

					if player then
						nearestPlayer = player
						torso = temp
					end
				--end
			end
		end
	end

	return torso
end

Attack:

if attackToChoose == ("Soul Possession") then
		--[[local function makeCoolEffect()
			local effect = vfxFolder:FindFirstChild("Winder"):Clone();
			effect.Parent = workspace.BossAttackParts
			effect.CFrame = rig.HumanoidRootPart.CFrame
			effect.Transparency = .1
			effect.Size = Vector3.new(25, 10, 25)
			effect.BrickColor = BrickColor.new("Really red")
			
			local effectTween = tweenService:Create(effect, TweenInfo.new(.25, Enum.EasingStyle.Linear), {Size = Vector3.new(15, 7, 15), Transparency = 1, Orientation = Vector3.new(0, math.random(1, 360), 0)})
			effectTween:Play()
			
			effectTween.Completed:Connect(function()
				effect:Destroy()
			end)
		end--]]

		local function makeCoolEffect2()
			local effect = vfxFolder:FindFirstChild("coolfgdksfkds"):Clone();
			effect.Parent = workspace.BossAttackParts
			effect.CFrame = rig.Torso.CFrame
			effect.Transparency = .1
			effect.Size = Vector3.new(1, .1, 1)
			effect.BrickColor = BrickColor.new("Black")

			local effectTween = tweenService:Create(effect, TweenInfo.new(.15, Enum.EasingStyle.Quad), {Size = Vector3.new(77, 2, 77), Transparency = 1})
			effectTween:Play()

			effectTween.Completed:Connect(function()
				effect:Destroy()
			end)
		end

		--[[local function makeCoolEffect3()
			local effect = vfxFolder:FindFirstChild("InwardShockwave"):Clone();
			effect.Parent = workspace.BossAttackParts
			effect.CFrame = rig.HumanoidRootPart.CFrame
			effect.Transparency = .85
			effect.Size = Vector3.new(25, 25, 25)
			effect.BrickColor = BrickColor.new("Really red")

			local effectTween = tweenService:Create(effect, TweenInfo.new(.25, Enum.EasingStyle.Quad), {Transparency = .9, Size = Vector3.new(2, 2, 2)})
			effectTween:Play()

			effectTween.Completed:Connect(function()
				effect:Destroy()
			end)
		end--]]

		local target = findNearestPlayer(rig.Torso.Position)

		if target ~= nil then
			target = findNearestPlayer(rig.Torso.Position)
			target = target

			animateScript.Enabled = false
			npcChat("Soul Possession", Enum.ChatColor.White)

			local bF = Instance.new("BodyVelocity");
			bF.Parent = rig.HumanoidRootPart
			bF.Velocity = Vector3.new(0, 0, 0)

			hum.WalkSpeed = 1
			inAirAnim:Play()
			animateScript.Enabled = false

			nearestPlayer = target

			task.wait(.4)

			local posTween = tweenService:Create(rig.HumanoidRootPart, TweenInfo.new(1, Enum.EasingStyle.Quad, Enum.EasingDirection.In), {CFrame = nearestPlayer.CFrame * CFrame.new(0, 15, 0)})
			posTween:Play()

			task.spawn(function()
				for stage = 1, 70 do
					task.wait(.035)
					makeCoolEffectForTrailInAir()
				end
			end)

			local newParticles = rig.Torso:FindFirstChild("SoulPossessionAttacks"):Clone();
			newParticles.Parent = rig.Torso
			newParticles.Name = "SoulPossessionAttacksNew"

			for _, particle in ipairs(rig.Torso.SoulPossessionAttacksNew:GetChildren()) do
				particle.Parent = rig.Torso
				if particle.Enabled == false then
					particle.Enabled = true
				end
			end

			posTween.Completed:Connect(function()
				if nearestPlayer ~= nil then
					makeCoolEffect2()

					--target = target
					
					local pos = Vector3.new(nearestPlayer.CFrame.Position.X, rig.HumanoidRootPart.CFrame.Position.Y + 10, nearestPlayer.CFrame.Position.Z);
					local cFrame = CFrame.new(pos)

					local target = {
						CFrame = cFrame
					}

					local trueTargetPos = target.CFrame.Position

					rig.HumanoidRootPart.CFrame = CFrame.new(rig.HumanoidRootPart.Position, trueTargetPos * Vector3.new(1, 0, 1) + Vector3.new(0, rig.HumanoidRootPart.Position.Y, 0))

					local posTween2 = tweenService:Create(rig.HumanoidRootPart, TweenInfo.new(.5, Enum.EasingStyle.Quad, Enum.EasingDirection.In), target)
					posTween2:Play()
					rig.Torso.SoulImpact:Play()

					posTween2.Completed:Connect(function()
						--[[target = findNearestPlayer(rig.Torso.Position)
						target = target--]]
						
						pos = Vector3.new(nearestPlayer.CFrame.Position.X, rig.HumanoidRootPart.CFrame.Position.Y + 10, nearestPlayer.CFrame.Position.Z);
						local cFrame = CFrame.new(pos)

						local target = {
							CFrame = cFrame
						}

						local trueTargetPos = target.CFrame.Position

						rig.HumanoidRootPart.CFrame = CFrame.new(rig.HumanoidRootPart.Position, trueTargetPos * Vector3.new(1, 0, 1) + Vector3.new(0, rig.HumanoidRootPart.Position.Y, 0))

						local posTween3 = tweenService:Create(rig.HumanoidRootPart, TweenInfo.new(.5, Enum.EasingStyle.Quad, Enum.EasingDirection.In), target)
						posTween3:Play()
						rig.Torso.SoulImpact:Play()

						posTween3.Completed:Connect(function()
							--[[target = findNearestPlayer(rig.Torso.Position)
							target = target--]]
							
							pos = Vector3.new(nearestPlayer.CFrame.Position.X, rig.HumanoidRootPart.CFrame.Position.Y + 10, nearestPlayer.CFrame.Position.Z);
							local cFrame = CFrame.new(pos)

							local target = {
								CFrame = cFrame
							}

							local trueTargetPos = target.CFrame.Position

							rig.HumanoidRootPart.CFrame = CFrame.new(rig.HumanoidRootPart.Position, trueTargetPos * Vector3.new(1, 0, 1) + Vector3.new(0, rig.HumanoidRootPart.Position.Y, 0))

							local posTween4 = tweenService:Create(rig.HumanoidRootPart, TweenInfo.new(.65, Enum.EasingStyle.Quad, Enum.EasingDirection.In), target)
							posTween4:Play()
							rig.Torso.SoulImpact:Play()

							posTween4.Completed:Connect(function()
								target = findNearestPlayer(rig.Torso.Position)

								for _, particle in ipairs(rig.Torso:GetChildren()) do
									if particle.Name == "SoulPossessionEffect" or particle.Name == "SoulPossessionEffect2" or particle.Name == "SoulPossessionEffect3" then
										particle:Destroy()
									end
								end

								task.wait(.4)

								local posTweenFinal = tweenService:Create(rig.HumanoidRootPart, TweenInfo.new(.45, Enum.EasingStyle.Quad, Enum.EasingDirection.In), {CFrame = rig.HumanoidRootPart.CFrame * CFrame.new(0, -45, 0)})
								posTweenFinal:Play()

								posTweenFinal.Completed:Connect(function()
									task.wait(.05)

									bF:Destroy()
									inAirAnim:Stop()

									local explosion = Instance.new("Part");
									explosion.Parent = workspace.BossAttackParts
									explosion.CFrame = rig.Torso.CFrame
									explosion.Name = "Explosion"
									explosion.Transparency = 1
									explosion.Size = Vector3.new(128, 55, 128)
									explosion.Anchored = true
									explosion.CanCollide = false
									explosion.Shape = Enum.PartType.Ball

									for _, particle in ipairs(rig.Torso.ExplosionEffect:GetChildren()) do
										spawn(function()
											local newParticle = particle:Clone();
											newParticle.Parent = explosion
											newParticle.Enabled = true

											newParticle:Emit(70)
											task.wait(.1)
											newParticle.Enabled = false
										end)
									end

								--[[local effect = rig.Torso.ExplosionEffect:Clone();
								effect.Parent = explosion
								effect.Explosion.Enabled = true--]]
								--[[effect.Explosion:Emit(5)
								effect.Explosion2:Emit(5)--]]

									rig.Torso.Explode:Play()

									local explosionHit = false;

									explosion.Touched:Connect(function(hit)
										rig.Torso.UltraKill.TimePosition = .1
										local playerHit = players:GetPlayerFromCharacter(hit.Parent);

										if playerHit then
											local char = playerHit.Character

											if char.Humanoid.Health > 0 and not char:FindFirstChildWhichIsA("ForceField") and explosionHit == false then
												explosionHit = true

												task.spawn(function()
													if char.Humanoid.Health > 0 and not char:FindFirstChildWhichIsA("ForceField") then
														canHeal = true
														char.Humanoid:TakeDamage(350)
														rig.Torso.UltraKill:Play()

														healthToHeal = 80000
														heal(healthToHeal)

														task.spawn(function()
															for _, bodyPart in ipairs(char:GetChildren()) do
																if bodyPart:IsA("Part") and bodyPart.Name == "Left Leg" or bodyPart.Name == "Right Leg" or bodyPart.Name == "Torso" or bodyPart.Name == "HumanoidRootPart" or bodyPart.Name == "Right Arm" or bodyPart.Name == "Left Arm" or bodyPart.Name == "Head" then
																	local bF2 = Instance.new("BodyVelocity");
																	bF2.Parent = bodyPart
																	bF2.MaxForce = explosion.CFrame.LookVector * 4500
																	bF2.Velocity = explosion.CFrame.LookVector * 4500

																	task.wait(.06)

																	bF2:Destroy()
																end
															end
														end)
													end
												end)
											end
										end
									end)

									ground:Play()
									hum.WalkSpeed = 0

									task.wait(1.35)

									ground:Stop()
									explosion:Destroy()

									hum.WalkSpeed = regularWalkSpeed
									animateScript.Enabled = true

									canHeal = true
									target = nil
								end)
							end)
						end)
					end)
				end
			end)
		end
	end

the nearestPlayer variable is the HumanoidRootPart.

Here’s a supporting video:

boosting!!!

nevermind, I found out the problem, I just had to switch to

rig.HumanoidRootPart.CFrame = CFrame.new(rig.HumanoidRootPart.Position, trueTargetPos * Vector3.new(1, 0, 1) + Vector3.new(0, rig.HumanoidRootPart.Position.Y, 0) + rig.HumanoidRootPart.CFrame.LookVector *Vector3.new(1,0,1)*0.01)

Mark this as the solution if this is correct.

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