Hello Devforum! I have been making a zombie recently, but I have run into an interesting error that I have found really annoying. Once you kill a zombie, an error appears saying that a part of the zombie is not a valid member.
Example:
This is a short video of what happens:
https://gyazo.com/d231f0d512c55f0dda389736b114d36d
I think this error appears because I have a while loop for every second that fires a function from the module of the zombie, so I made the zombie get destroyed quicker, but the error still occurred. It might have something to do with my module script or my main script, but I am not sure where it goes wrong.
Script:
local pathfindingservice = game:GetService("PathfindingService")
local collectionservice = game:GetService("CollectionService")
local serverstorage = game:GetService("ServerStorage")
local zombies = {}
local serverzombie = serverstorage:WaitForChild("Zombie")
local zombiemodule = require(game.ReplicatedStorage.ZombieModule)
function taghuman(instance)
local human = instance:FindFirstChildWhichIsA("Humanoid")
if human then
collectionservice:AddTag(human, "Human")
end
end
function tagzombie(instance)
local zombie = instance:FindFirstChild("Zombie")
if zombie then
collectionservice:AddTag(zombie, "Zombie")
end
end
game.Workspace.ChildAdded:Connect(taghuman)
print("tagged human")
game.Workspace.ZombieFolder.ChildAdded:Connect(tagzombie)
print("tagged zombie")
while true do
local zombieclone = serverzombie:Clone()
zombieclone.UpperTorso.CFrame = game.Workspace.Spawners.Spawner.CFrame
zombieclone.Parent = game.Workspace.ZombieFolder
tagzombie(zombieclone)
print("tagged zombie")
zombiemodule.healthfunction(zombieclone)
print("Health gui in action")
zombiemodule.findtarget(zombieclone)
print("Found target for "..zombieclone.Name)
coroutine.wrap(function()
while wait(1) do
if zombieclone then
zombiemodule.movementcontrol(zombieclone)
end
end
end)()
wait(5)
end
function initialize()
for i, v in pairs(game:GetChildren()) do
taghuman(v)
print("tagged human")
end
end
initialize()
Module:
local module = {}
local collectionservice = game:GetService("CollectionService")
local pathfindingservice = game:GetService("PathfindingService")
module.findtarget = function(zombie)
local humans = collectionservice:GetTagged("Human")
local zomhumroot = zombie.HumanoidRootPart
local dist = 1000
local target = nil
for i, human in pairs(game.Workspace:GetChildren()) do
if human:FindFirstChild("Humanoid") and human:FindFirstChild("HumanoidRootPart") then
local humanoid = human:FindFirstChild("Humanoid")
local humroot = human:FindFirstChild("HumanoidRootPart")
if humanoid and humroot and human ~= zombie then
if humanoid.Health > 0 then
if (zomhumroot.Position - humroot.Position).magnitude < dist then
dist = (zomhumroot.Position - humroot.Position).magnitude
target = humroot
end
end
end
end
end
return target
end
module.movementcontrol = function(zombie)
local zomhumroot = zombie.HumanoidRootPart
local zomanoid = zombie.Zombie
local anim = zomanoid.Walk
local anim2 = zomanoid.Attack
local anim3 = zomanoid.Idle
local loadanim = zomanoid:LoadAnimation(anim)
local loadanim2 = zomanoid:LoadAnimation(anim2)
local loadanim3 = zomanoid:LoadAnimation(anim3)
local humroot = module.findtarget(zombie)
if humroot then
zomanoid:MoveTo(humroot.Position, humroot)
loadanim:Play()
if (zomhumroot.Position - humroot.Position).magnitude < 5 then
loadanim2:Play()
humroot.Parent.Humanoid:TakeDamage(15)
end
else
zomanoid:MoveTo(zomhumroot.Position + Vector3.new(math.random(-50,50),0, math.random(-50,50)), game.Workspace.Ground)
loadanim:Stop()
loadanim3:Play()
wait(3)
loadanim3:Stop()
loadanim:Play()
end
end
module.healthfunction = function(zombie)
local healthgui = zombie.Head:FindFirstChild("HealthGui")
local zomanoid = zombie:FindFirstChild("Zombie")
zomanoid:GetPropertyChangedSignal("Health"):Connect(function()
local healthchange = zomanoid.Health/zomanoid.MaxHealth
local healthcolor = Color3.fromRGB(255,0,0):Lerp(Color3.fromRGB(60,255,0),healthchange)
healthgui.Health.Meter:TweenSize(UDim2.new(healthchange,0,1,0),"In","Linear",1)
healthgui.Health.Meter.BackgroundColor3 = healthcolor
if zomanoid.Health <= 0 then
wait(1)
zombie:Destroy()
end
end)
end
return module
What do I do?