Script breaks when player died

Hello ,

I have some problems with my NPC script . the script working fine with the first NPC but the script break with the cloned NPC . It happen when cloned NPC died after player died or reset . Also my script is a bit long and quite messy I guess…

script:

local Collection = game:GetService("CollectionService")
local Run = game:GetService("RunService")
local PathFinding = game:GetService("PathfindingService")

local Fast = require(game.ReplicatedStorage.Module.FastWait)

local playercon
local characterAdded
local characterdead

local RunCon
local TouchedCon
local DeadCon

local newchilds
local childsRun
local childsTouched
local childsdead

local Cooldown = false

local spawner = game.Workspace.way:GetChildren()
local mobspawn = spawner[math.random(1 , #spawner)]

local mobfold = workspace.Folder

playercon = game.Players.PlayerAdded:Connect(function(player)
	characterAdded = player.CharacterAdded:Connect(function(char)
		
		for _ , tag in pairs(Collection:GetTagged("Mob")) do
			
		
			TouchedCon = tag.HumanoidRootPart.Touched:Connect(function(hit)

				if hit.Parent:FindFirstChild("Humanoid") then

					if Cooldown == false then

						Cooldown = true

						hit.Parent:FindFirstChild("Humanoid"):TakeDamage(15)

						Fast(2)

						Cooldown = false

					else print("no yet") end
				end
			end)

			RunCon = Run.Stepped:Connect(function(dt)

				if (char:WaitForChild("HumanoidRootPart").Position - tag:WaitForChild("HumanoidRootPart").Position).Magnitude < 15 then

					local path = PathFinding:CreatePath()

					path:ComputeAsync(tag.HumanoidRootPart.Position , char:WaitForChild("HumanoidRootPart").Position)

					local waypoints = path:GetWaypoints()

					for i , way in pairs(waypoints) do

						tag.Humanoid:MoveTo(way.Position)

						tag.Humanoid.MoveToFinished:Wait()
					end

				else

					local path = PathFinding:CreatePath()

					path:ComputeAsync(tag.HumanoidRootPart.Position , mobspawn.Position)

					local waypoints = path:GetWaypoints()

					for i , way in pairs(waypoints) do

						tag.Humanoid:MoveTo(way.Position)

						tag.Humanoid.MoveToFinished:Wait()
					end
				end
			end)
				
				
			DeadCon = tag.Humanoid.Died:Connect(function()
				
					
					TouchedCon:Disconnect()
					RunCon:Disconnect()
					
					local clone = game.ReplicatedStorage.MobFolder.Bandits:Clone()
					clone.Parent = workspace.Folder
					clone:SetPrimaryPartCFrame(mobspawn.CFrame)
					
					Collection:GetInstanceAddedSignal(clone)
					
					Fast(2)
					
					tag:Destroy()
					
					Collection:RemoveTag(tag)
					
					Collection:GetInstanceRemovedSignal(tag)
					
					print(RunCon.Connected , TouchedCon.Connected)
					
					DeadCon:Disconnect()
					
			end)
			
			
		end
		
		newchilds = Collection:GetInstanceAddedSignal("Mob"):Connect(function(new)

			print("ok")


			childsTouched = new.HumanoidRootPart.Touched:Connect(function(hit)

				if hit.Parent:FindFirstChild("Humanoid") then

					if Cooldown == false then

						Cooldown = true

						hit.Parent:FindFirstChild("Humanoid"):TakeDamage(15)

						Fast(2)

						Cooldown = false

					else print("no yet") end
				end
			end)

			childsRun = Run.Stepped:Connect(function(dt)

				if (char:WaitForChild("HumanoidRootPart").Position - new:WaitForChild("HumanoidRootPart").Position).Magnitude < 15 then

					local path = PathFinding:CreatePath()

					path:ComputeAsync(new.HumanoidRootPart.Position , char:WaitForChild("HumanoidRootPart").Position)

					local waypoints = path:GetWaypoints()

					for i , way in pairs(waypoints) do

						new.Humanoid:MoveTo(way.Position)

						new.Humanoid.MoveToFinished:Wait()
					end

				else

					local path = PathFinding:CreatePath()

					path:ComputeAsync(new.HumanoidRootPart.Position , mobspawn.Position)

					local waypoints = path:GetWaypoints()

					for i , way in pairs(waypoints) do

						new.Humanoid:MoveTo(way.Position)

						new.Humanoid.MoveToFinished:Wait()
					end
				end
			end)


			childsdead = new.Humanoid.Died:Connect(function()
				
				childsTouched:Disconnect()
				childsRun:Disconnect()
				
				Fast()

				local clone = game.ReplicatedStorage.MobFolder.Bandits:Clone()
				clone.Parent = workspace.Folder
				clone:SetPrimaryPartCFrame(mobspawn.CFrame)

				Collection:GetInstanceAddedSignal(clone)

				Fast(2)

				new:Destroy()

				Collection:RemoveTag(new)

				Collection:GetInstanceRemovedSignal(new)

				print(RunCon.Connected , TouchedCon.Connected)

				childsdead:Disconnect()

			end)
			
			
			char:WaitForChild("Humanoid").Died:Connect(function()
				newchilds:Disconnect()
				childsRun:Disconnect()
				childsTouched:Disconnect()
			end)
		end)
		
		
		characterdead =	char:WaitForChild("Humanoid").Died:Connect(function()
			
			TouchedCon:Disconnect()
			RunCon:Disconnect()
			DeadCon:Disconnect()
			characterdead:Disconnect()
		end)
	end)
	playercon:Disconnect()
end)

feel free to tell me if you need anything . Any help is appriciated :slight_smile:

1 Like

A solution is to have a already working copy of the NPC model inside ServerStorage, and then every time a new one is needed, you can clone it and insert it into the game dynamically. The model should never be cloned after you need to regenerate it.

This will greatly simplify your code, and make it a lot easier to manage.

So I just parented it to workspace ??

Whenever you would need it, yup! Scripts don’t execute in ServerStorage, so all you’d need to do is just clone the model and parent it to Workspace or anywhere inside it that you’d need it to be.

After 1 hour I still cant’ make it done . I even got error Humanoid is not a valid member of Model after I disconnecting the event . I got error on this line

tag.Humanoid:MoveTo(way.Position) -- on line 81

and

Infinite yield possible on 'Bandits:WaitForChild("HumanoidRootPart") -- on line 56

this is the current script:

local Collection = game:GetService("CollectionService")
local Run = game:GetService("RunService")
local PathFinding = game:GetService("PathfindingService")

local Fast = require(game.ReplicatedStorage.Module.FastWait)

local playercon
local characterAdded
local characterdead

local RunCon
local TouchedCon
local DeadCon

local newchilds
local childsRun
local childsTouched
local childsdead

local Cooldown = false

local spawner = game.Workspace.way:GetChildren()
local mobspawn = spawner[math.random(1 , #spawner)]

local mobready = game.ServerStorage.MobReady:GetChildren()
local randommob = mobready[math.random(1 , #mobready)]

local mobfold = workspace.Folder

playercon = game.Players.PlayerAdded:Connect(function(player)
	characterAdded = player.CharacterAdded:Connect(function(char)
		
		for _ , tag in pairs(Collection:GetTagged("Mob")) do
			
		
			TouchedCon = tag.HumanoidRootPart.Touched:Connect(function(hit)

				if hit.Parent:FindFirstChild("Humanoid") then

					if Cooldown == false then

						Cooldown = true

						hit.Parent:FindFirstChild("Humanoid"):TakeDamage(15)

						Fast(2)

						Cooldown = false

					else print("no yet") end
				end
			end)

			RunCon = Run.Stepped:Connect(function(dt)

				if (char:WaitForChild("HumanoidRootPart").Position - tag:WaitForChild("HumanoidRootPart").Position).Magnitude < 15 then

					local path = PathFinding:CreatePath()

					path:ComputeAsync(tag.HumanoidRootPart.Position , char:WaitForChild("HumanoidRootPart").Position)

					local waypoints = path:GetWaypoints()

					for i , way in pairs(waypoints) do

						tag.Humanoid:MoveTo(way.Position)

						tag.Humanoid.MoveToFinished:Wait()
					end

				else

					local path = PathFinding:CreatePath()

					path:ComputeAsync(tag.HumanoidRootPart.Position , mobspawn.Position)

					local waypoints = path:GetWaypoints()

					for i , way in pairs(waypoints) do

						tag.Humanoid:MoveTo(way.Position)

						tag.Humanoid.MoveToFinished:Wait()
					end
				end
			end)
				
				
			DeadCon = tag.Humanoid.Died:Connect(function()
					
				TouchedCon:Disconnect()
				RunCon:Disconnect()
				
				Collection:RemoveTag(tag , "Mob")
				
				randommob.Parent = mobfold
				randommob:SetPrimaryPartCFrame(mobspawn.CFrame)
				print(randommob:WaitForChild("HumanoidRootPart") , randommob:WaitForChild("Humanoid"))
				
				local clone = game.ReplicatedStorage.MobFolder.Bear:Clone()
				clone.Parent = game.ServerStorage.MobReady
				print(clone:WaitForChild("HumanoidRootPart") , clone:WaitForChild("Humanoid"))
					
				Collection:GetInstanceAddedSignal(randommob)
					
				Fast(2)
					
				tag:Destroy()
				
				Collection:GetInstanceRemovedSignal(tag)
					
				print(RunCon.Connected , TouchedCon.Connected)
					
				DeadCon:Disconnect()
					
			end)
			
			
		end
		
		newchilds = Collection:GetInstanceAddedSignal("Mob"):Connect(function(new)

			print("ok")


			childsTouched = new.HumanoidRootPart.Touched:Connect(function(hit)

				if hit.Parent:FindFirstChild("Humanoid") then

					if Cooldown == false then

						Cooldown = true

						hit.Parent:FindFirstChild("Humanoid"):TakeDamage(15)

						Fast(2)

						Cooldown = false

					else print("no yet") end
				end
			end)

			childsRun = Run.Stepped:Connect(function(dt)

				if (char:WaitForChild("HumanoidRootPart").Position - new:WaitForChild("HumanoidRootPart").Position).Magnitude < 15 then

					local path = PathFinding:CreatePath()

					path:ComputeAsync(new.HumanoidRootPart.Position , char:WaitForChild("HumanoidRootPart").Position)

					local waypoints = path:GetWaypoints()

					for i , way in pairs(waypoints) do

						new.Humanoid:MoveTo(way.Position)

						new.Humanoid.MoveToFinished:Wait()
					end

				else

					local path = PathFinding:CreatePath()

					path:ComputeAsync(new.HumanoidRootPart.Position , mobspawn.Position)

					local waypoints = path:GetWaypoints()

					for i , way in pairs(waypoints) do

						new.Humanoid:MoveTo(way.Position)

						new.Humanoid.MoveToFinished:Wait()
					end
				end
			end)


			childsdead = new.Humanoid.Died:Connect(function()
				
			    childsTouched:Disconnect()
			    childsRun:Disconnect()
				
				Fast()

				randommob.Parent = mobfold
				randommob:SetPrimaryPartCFrame(mobspawn.CFrame)
				print(randommob:WaitForChild("HumanoidRootPart") , randommob:WaitForChild("Humanoid"))

				Collection:GetInstanceAddedSignal(randommob)
				
				local clone = game.ReplicatedStorage.MobFolder.Bear:Clone()
				clone.Parent = game.ServerStorage.MobReady
				print(clone:WaitForChild("HumanoidRootPart") , clone:WaitForChild("Humanoid"))


				Fast(2)

				new:Destroy()

				Collection:RemoveTag(new)

				Collection:GetInstanceRemovedSignal(new)

				print(RunCon.Connected , TouchedCon.Connected)

				childsdead:Disconnect()

			end)
			
			char:WaitForChild("Humanoid").Died:Connect(function()
				newchilds:Disconnect()
				childsRun:Disconnect()
				childsTouched:Disconnect()
			end)
		end)
		
		
		characterdead =	char:WaitForChild("Humanoid").Died:Connect(function()
			
			TouchedCon:Disconnect()
			RunCon:Disconnect()
			DeadCon:Disconnect()
			characterdead:Disconnect()
		end)
	end)
	playercon:Disconnect()
end)