I’m working on script for a character that kills other characters. Once the player dies i got this error when the players been removed from the workspace…
local zombieTorso = script.Parent.Torso
local zombieHumanoid = script.Parent.Humanoid
local function findTarget()
local agroDistance = 100
local target = nil
for i, v in pairs(game.Workspace:GetChildren()) do
local human = v:FindFirstChild("Humanoid")
local torso = v:FindFirstChild("Torso")
if human and torso and v~= script.Parent then
-- Check distance
if (zombieTorso.Position - torso.Position).magnitude < agroDistance then
agroDistance = (zombieTorso.Position - torso.Position).magnitude
target = torso
end
end
end
return target
end
while wait(2) do
local torso = findTarget()
if torso then
zombieHumanoid:MoveTo(torso.Position)
for i, v in pairs(game.Workspace:GetChildren()) do
zombieHumanoid.Touched:Connect(function(v)
local human = v.Parent:FindFirstChild("Humanoid")
if human then
human:TakeDamage(human.MaxHealth)
wait(3)
local characterModel = human.Parent
characterModel:Destroy()
end
end)
end
else
zombieHumanoid:MoveTo(zombieTorso.Position + Vector3.new(math.random(-50,50), 0, math.random(-50,50), 0))
end
end