AI enemies vanishing on death

I’m currently using a module called SimplePath. I’m not that experienced in pathfinding so I thought I’d leave most of the back end up to a module.

I’m attempting to make a basic enemy that will wander around in random directions, when a player gets in range it will chase them. They can be shot and killed and used in a wave survival scenario.

The only problem is I’m having an issue where when the Humanoid dies, the rig has a chance to disappear. I have added a ragdoll on death but when removing it still does the same thing.

The original post in the forum is here to see the video of it happening.

I’ve heard suggestions that I need to minimize how often I’m using the :Run function and run all the enemies via one script (I’m already running the code via a module script for handling enemies)

Here is my code:

function class:Start()
	local rig : Model = self.Rig
	local humanoid : Humanoid = self.Rig.Humanoid
	local radius = 20
	local player = nil
	local isFollowing = false
	local followRange = 80 -- Adjust the follow range as needed
	local nearestPlayer

	local Path = simplePath.new(rig)

	Path.Visualize = true

	self.Walk:Play()
	
	humanoid.StateChanged:Connect(function(old, new)
		if new == Enum.HumanoidStateType.Dead then
			self.Idle:Stop()
			self.Walk:Stop()
			Path:Destroy()
		end
	end)
	
	local function followPlayer()
		if rig.Humanoid.Health > 0 then
			nearestPlayer = findNearestPlayer(followRange, rig.PrimaryPart)

			if nearestPlayer then
				isFollowing = true

				--Cancel Current Path
				if Path.StatusType.Active == true then
					Path:Stop()
				end

				Path:Run(nearestPlayer.PrimaryPart.Position)
			else
				isFollowing = false
			end
		end
	end
	
	local function Wander()
		--Cancel Current Path
		if Path.StatusType.Active == true then
			Path:Stop()
		end
		
		self.Walk:Stop()
		self.Idle:Play()
		wait(math.random(2, 5))
		self.Idle:Stop()
		self.Walk:Play()
		Path:Run(rig.PrimaryPart.Position + Vector3.new(math.random(-radius, radius), 0 , math.random(-radius, radius)))
	end

	-- Check for nearby players periodically
	task.spawn(function()
		while true do
			if not isFollowing then
				followPlayer()
			end
			wait() \
		end
	end)

	-- Wandering
	Path.Reached:Connect(function()
		if not isFollowing then
			Wander()
		else
			if not self.Walk.IsPlaying then
				self.Idle:Stop()
				self.Walk:Play()
			end
			
			Path:Run(nearestPlayer.PrimaryPart.Position)
		end
	end)
	
	Path.WaypointReached:Connect(function()
		if isFollowing then
			if not self.Walk.IsPlaying then
				self.Idle:Stop()
				self.Walk:Play()
			end
			
			Path:Run(nearestPlayer.PrimaryPart.Position)
		end
	end)
	
	Path.Error:Connect(function(errorType)
	--	warn(errorType)
		if isFollowing then
			if not self.Walk.IsPlaying then
				self.Idle:Stop()
				self.Walk:Play()
			end
			
			Path:Run(nearestPlayer.PrimaryPart.Position)
		else
			Wander()
		end
	end)
	
	--Dummy knows to compute path again if something blocks the path
	Path.Blocked:Connect(function()
		if isFollowing then
			if not self.Walk.IsPlaying then
				self.Idle:Stop()
				self.Walk:Play()
			end
			
			Path:Run(nearestPlayer.PrimaryPart.Position)
		else
			if Path.StatusType.Active == true then
				Path:Stop()
			end	
			
			Path:Run(rig.PrimaryPart.Position + Vector3.new(math.random(-radius, radius), 0, math.random(-radius, radius)))
		end
	end)

	-- Start wandering initially
	Path:Run(rig.PrimaryPart.Position + Vector3.new(math.random(-radius, radius), 0, math.random(-radius, radius)))
end