Script still activating after its parent destroyed

So i have a script that is parented to a Monster model. The problem is after i destroy the monster its script keeps on running and keeps spawning some Blast attack even if i coudn’t see any script on the workspace.

local Npc = script.Parent
local zombieHumanoid = script.Parent:FindFirstChild("Humanoid")
local animation = script:FindFirstChildOfClass("Animation")
local walkAnimation = script:FindFirstChild("walkAnimation")
local laughAnimation = script:FindFirstChild("LaughAnimation")
local animator = zombieHumanoid:FindFirstChildOfClass("Animator")
local animationTrack = animator:LoadAnimation(animation)
local walk = animator:LoadAnimation(walkAnimation)
local laugh = animator:LoadAnimation(laughAnimation)
local zombieHRP = script.Parent:FindFirstChild("HumanoidRootPart")
local module = require(game.ServerStorage["MuchachoHitboxV1.1"])
local SmahSound = script:FindFirstChild("Sound")
local power = game.ServerStorage.power
local blast = game.ServerStorage.Blast
local hitbox = module.CreateHitbox()
local db = false
walk:Play()
local function findTarget()
	local distance = 500
	local target = nil
	for i,v in pairs(game.Workspace:GetChildren()) do
		local human = v:FindFirstChild("Humanoid")
		local HumanoidRootPart = v:FindFirstChild("HumanoidRootPart")
		if human and HumanoidRootPart and v ~= script.Parent and human.Health > 0 then
			if(zombieHRP.Position- HumanoidRootPart.Position).Magnitude < distance then
				distance = (zombieHRP.Position- HumanoidRootPart.Position).Magnitude
				target = HumanoidRootPart
				if distance < 80 and not db then
					db = true
					animationTrack:Play()
					SmahSound:Play()
					local powerclone = power:Clone()
					local blastclone = blast:Clone()
					powerclone.Parent = game.Workspace
					blastclone.Parent = game.Workspace
					powerclone.Position = zombieHRP.Position + Vector3.new(0,-68,0)
					blastclone.Position = zombieHRP.Position + Vector3.new(0,-68,0)
					hitbox.Size = Vector3.new(5,5,5)
					hitbox.CFrame = powerclone
					for i=1,10,1 do
						hitbox.Size = hitbox.Size + Vector3.new(13,8,13)
					end
					for i=1,10,1 do
						task.wait(0.001)
						blastclone.Size  = blastclone.Size + Vector3.new(13,0,13)
					end
					hitbox.Touched:Connect(function(hit,hum)
						if hum.Parent.Name ~= "Monster" then
							hum:TakeDamage(50)
						end
					end)
					hitbox:Start()
					task.wait(0.5)
					blastclone:Destroy()
					hitbox:Stop()
					walk:Stop()
					laugh:Play()
					task.wait(4)
					powerclone:Destroy()
					db = false
					walk:Play()
					laugh:Stop()
				end
			end
		end
	end
	if zombieHumanoid.Health <= 0  then
		script.Disabled = true
		Npc:Destroy()
	end
	return target
end

while wait(1) do
	local HumanoidRootPart = findTarget()
	if HumanoidRootPart then
		zombieHumanoid:MoveTo(HumanoidRootPart.Position)
	else
		zombieHumanoid:MoveTo(zombieHRP.Position + Vector3.new(math.random(-50,30),0,0))
	end
	
end


![image|666x500]

robloxapp-20221117-1759410.wmv (3.3 MB)

Disable the script first then delete the Monster model.
Also please give more details about this.

local monsterScript = monster.Script
monsterScript.Disabled = true
monster:Destroy()
1 Like

can you show us your script?vvv

1 Like

It’s Enabled now and you really shouldn’t do it this way, but, yeah, can we see some code, @IvanoZ?

Oh, i forgot bruh (epicrapbattles)

Hi i already edited the post and add some details about it

As you can see the first image, the monster had its blast attack which is indicated as a red box. after the monsters health < 0 so i decided to destroy the monster. But in the second image shows the blast attack are still there and looping every 4 seconds.

The while() loop will continue to execute because it is running in memory, even if you Disable the script it will only prevent it from executing again (i.e. from creating another while() loop). Maybe try this:

local isAlive = true;

Then in your findTarget() code switch isAlive to false:

	if zombieHumanoid.Health <= 0  then
		isAlive = false;
	end
while wait(1) do
	if isAlive == false then
		break;
	end

	-- rest of code

end

Npc:Destroy();

The script is parented to the NPC so once the NPC is destroyed it should end the script. And since the script will spawn in each time the NPC does it will execute the script everytime on creation.

1 Like

i really appreciate to all who helped me. But i had a solution already i only put a “break” inside the while loop get rid of the problem.

Gah, self-awarded solutions! Has nothing anyone has said not provided anything at all to you?

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