Enemies sometimes get deleted

  1. What do you want to achieve? Keep it simple and clear!
    Basically i have this enemy that attacks other npcs
  2. What is the issue? Include screenshots / videos if possible!
    When the enemy kills an npc, it gets deleted for no reason, i have checked the code but so far nothing seems to be deleting it
  3. What solutions have you tried so far? Did you look for solutions on the Developer Hub?
    i have tried rescripting and cleaning the code more, but still the same results.

Any help is appreciated

It’s hard to solve this problem without context. Can you send the code?

local rs = game:GetService("ReplicatedStorage")
local bullet = rs.bullet
local npc = script.Parent
local atak = script:WaitForChild("attack")
local atack = npc.Humanoid:LoadAnimation(atak)
local db = false
local use1 = true
local use2 = true
local use3 = true
local canshoot = true

local hrp = npc:WaitForChild("HumanoidRootPart")

local maxDistance = 200
wait(0.2)
while wait() do
	local plrs = workspace:GetChildren()
	local npctokill
	for i, plr in pairs(plrs) do
		local monster = plr:FindFirstChild("ismonster")
		local death = plr:FindFirstChild("isdead")
		local evil = plr:FindFirstChild("evil")
		if plr then
			if monster then
				if death then
					if evil then
						if plr:FindFirstChild("HumanoidRootPart") and death.Value == false then


							local hrp = plr:WaitForChild("HumanoidRootPart")
							local hrp = plr.HumanoidRootPart

							if not npctokill then 
								npctokill = hrp 
							end
						end
					end
				end
			end
		end
		if npctokill and (hrp.Position - npctokill.Position).Magnitude <= maxDistance and npc.isdead.Value == false then 	
			local target = npctokill.Position
			npc:SetPrimaryPartCFrame(CFrame.lookAt(hrp.Position, npctokill.Position * Vector3.new(1, 0, 1) + hrp.Position * Vector3.new(0, 1, 0)))
			npc.Humanoid:MoveTo(target - CFrame.new(npc.HumanoidRootPart.Position, target).LookVector * 25)

		end

	end	


	spawn(function()
		while true do
			wait(math.random(3,5))
			if npctokill ~= nil then
				if db == false and npc.isdead.Value == false then
					if canshoot == true   then
						db = true
						atack:Play()
						for i = 1, 3 do
							wait()
							local balclone1 = bullet:Clone()
							balclone1.Parent = workspace
							balclone1.CFrame = hrp.Parent.me.CFrame * CFrame.new(0,0,-6)
							if use1 == true then
								use1 = false		
								local v1 = Instance.new("BodyVelocity")
								v1.Parent = balclone1
								v1.MaxForce = Vector3.new(99999,99999,99999)
								v1.P = 2350
								v1.Velocity = hrp.Parent.me.CFrame.LookVector * 140
								spawn(function()
									wait(0.6)
									v1:Destroy()
								end)
							elseif use1 == false and use2 == true then
								use2 = false
								local v2 = Instance.new("BodyVelocity")
								v2.Parent = balclone1
								v2.MaxForce = Vector3.new(99999,99999,99999)
								v2.P = 2350
								v2.Velocity = hrp.Parent.me2.CFrame.LookVector * 140
								spawn(function()
									wait(0.6)
									v2:Destroy()
								end)
							elseif use1 == false and use2 == false and use3 == true then
								use3 = false
								local v3 = Instance.new("BodyVelocity")
								v3.Parent = balclone1
								v3.MaxForce = Vector3.new(99999,99999,99999)
								v3.P = 2350
								v3.Velocity = hrp.Parent.me3.CFrame.LookVector * 140
								spawn(function()
									wait(0.6)
									v3:Destroy()
								end)

								use1 = true
								use2 = true
								use3 = true
							end
						end

						wait(2)
						db = false
					end
				end
			end
		end
	end)
end

Nevermind. i had a setting called StreamingEnabled, sorry for the inconvenience