I have some AI enemies that deal damage to me and I have to try and kill them, after I die they respawn. I can kill them before they respawn but after they respawn my sword script breaks. I get this error: "Zombie is not a valid member of Model" on Line 11. (Zombie is the humanoid inside my enemy, Dark,) I think this is because my respawn script removes the enemy before cloning it back into the world. In that time my sword script errors because there is no enemy. Although when the enemy gets cloned back my sword script still errors?
Enemy Cloning Script
z = script.Parent backup = z:clone() game:GetService('Players').PlayerAdded:connect(function(player) player.CharacterAdded:connect(function(character) character:WaitForChild("Humanoid").Died:connect(function() z:Remove() wait(2) backup.Parent = game.Workspace.Enemies backup.Head:MakeJoints() backup.Torso:MakeJoints() wait(15) z.Zombie.WalkSpeed = 16 end) end) if z == nil then wait(4) backup.Parent = game.Workspace.Enemies backup.Head:MakeJoints() backup.Torso:MakeJoints() end end)
Sword script
local enemies = game.Workspace.Enemies:GetChildren() --// All the children of the enemies folder. for i, v in pairs(enemies) do if v:FindFirstChild("Zombie") then --// If the model has a humanoid wait(1) v.HumanoidRootPart.Touched:Connect(function()--// When the sword touches the npc script.Parent.Activated:Connect(function() --// If you click holding the sword it will damage. v.Zombie:TakeDamage(10) --// Npc takes 10 Damage wait(1) end) end) end end
I tried to add at the beginning of the sword script (didn't work)
while game.Workspace:FindFirstChild("Enemies") do
Thanks!