Cloned Enemies Sharing Scripts/Health

Im trying to clone 3 enemies and have them attack you, ive gotten pretty far and been able to clone them, teleport them to a specific location, rotate them and the player can kill them. how the script that clones the enemies works is:

  1. it clones the enemies from one enemy that sits outside the map with all the scripts and stuff to make the enemy work

  2. it teleports the cloned enemies to specific locations and rotates them

The problem is that when i kill one of the enemies, the next cloned enemies instantly die or just dissapear. When i kill an enemy i remove their humanoid to hide its name and health, i dont think removing the dead enemies humanoid is causing the problem but i thought id mention it.

I want to stop the cloned enemies from dying as soon as they spawn

I have tried a few things, but they all have the same problem that the cloned enemies die, ive tried googling a solution but couldnt find anything, ive tried to use chatgpt to figure out how to solve it but that didnt work. Ive also thought about having like 30 enemies outside the map and teleporting a few in but that would probably make the game very laggy.

I have basically never scripted, but im trying to learn it by making a simple game.

here are the scripts:

script that clones the enemies and teleports them

local modeltoclone = game.Workspace.CloningEnemy --enemy outside map
local newclone1 = modeltoclone:Clone() --first clone
local newclone2 = modeltoclone:Clone() --second clone
local newclone3 = modeltoclone:Clone() --third clone

-- there is some stuff in between here but i removed it since it isnt important

	newclone1.Name = "Bee" --changes name
	newclone1:SetPrimaryPartCFrame(CFrame.new(-20.383, 2.376, 211.419)) --teleports cloned enemy
	rotation = CFrame.Angles(0, math.rad(25), 0) --this line and the 2 under it rotate the cloned enemy
	ModelCF =  newclone1.PrimaryPart.CFrame
	newclone1:SetPrimaryPartCFrame(ModelCF * rotation)
	wait() --under this, the same thing is done for 2 more cloned enemies, copy paste of the code above, just position and rotation changed

enemy death script

while true do
	wait(0.1)
	if script.Parent.Humanoid.Health<1 then
		script.Parent.Head.Damage.Enabled = false --disables script that deals damage
		idle:Stop()
		local chance = math.random(1,3) --picks random number to choose what parts to disconnect from body (this is unnecessary but id like to keep it if possible)
		if chance == 1 then
			script.Parent.HumanoidRootPart["Left Wing"]:Destroy()
			script.Parent.HumanoidRootPart.Head:Destroy()
			script.Parent.Humanoid:Destroy() --this could maybe be causing it?
			wait(20)
			script.Parent.Tail:Destroy() -- this line and the 3 under it just destroy some parts, leaving just the head and a wing together with a few scripts
			script.Parent["Right Wing"]:Destroy()
			script.Parent["Rear Body"]:Destroy()
			script.Parent["Front Body"]:Destroy()
			wait(40)
			script.Parent:Destroy() --this destroys the entire model
		end
		if chance == 2 then
			--copy paste of the code above, but first 2 lines are changed to be other parts
		end
		if chance == 3 then
			--copy paste of the code above, but first 2 lines are changed to be other parts
		end
	end
end

i might have forgotten to add something, i havent posted anything on here in a while.

1 Like

Have you tried putting the enemy in ReplicatedStorage or ServerStorage and clone from it?

local modeltoclone = game.ReplicatedStorage.CloningEnemy --enemy outside map
local newclone1 = modeltoclone:Clone() --first clone
local newclone2 = modeltoclone:Clone() --second clone
local newclone3 = modeltoclone:Clone() --third clone
...

I forgot to mention it, but yes i tried it with the cloningenemy in replicated storage and i had the same problem, altough maybe i did it wrong, ill try it again. Also thanks for replying!

And I don’t recommend using while true (as it makes an unnecessary burden), it’s better to use HealthChanged
Example:

local died=false
script.Parent.Humanoid.HealthChanged:Connect(function(health)
	if health<=1 and not died then
		died=true
		script.Parent.Head.Damage.Enabled = false --disables script that deals damage
		idle:Stop()
		local chance = math.random(1,3) --picks random number to choose what parts to disconnect from body (this is unnecessary but id like to keep it if possible)
		if chance == 1 then
			script.Parent.HumanoidRootPart["Left Wing"]:Destroy()
			script.Parent.HumanoidRootPart.Head:Destroy()
			script.Parent.Humanoid:Destroy() --this could maybe be causing it?
			wait(20)
			script.Parent.Tail:Destroy() -- this line and the 3 under it just destroy some parts, leaving just the head and a wing together with a few scripts
			script.Parent["Right Wing"]:Destroy()
			script.Parent["Rear Body"]:Destroy()
			script.Parent["Front Body"]:Destroy()
			wait(40)
			script.Parent:Destroy() --this destroys the entire model
		end
		if chance == 2 then
			--copy paste of the code above, but first 2 lines are changed to be other parts
		end
		if chance == 3 then
			--copy paste of the code above, but first 2 lines are changed to be other parts
		end
	end
end)

Ive figured out the problem, as you can see in this video:

I think what its doing is taking the dead enemy (alive in the video) and just teleporting it back without cloning a new enemy, im going to try and fix this and change the while true to healthchanged like you said.

2 Likes