Help with zombie infection script

What’s the issue?
Zombie’s who were zombified inside of the zombify function aren’t being turned back to normal

What does the script already do correctly?

  • Changes the texture of the face
  • Changes the body colors of the zombie
  • Adds a isZombie tag to the humanoid of the zombie
  • Transforms the player who was transformed by the zombify
    function outside of the zombify function to a human correctly
  • After being transformed back to a human the player can still infect people

Here’s my code (it’s kind of messy):

local function zombify(character)
if not character then
return
end
local zh = character:WaitForChild("Humanoid")
local isZombie = Instance.new("BoolValue", zh)
isZombie.Name = "isZombie"
isZombie.Value = true
local oldColors = character:WaitForChild("Body Colors")
local copyColors = oldColors:clone()
copyColors.Parent = game:GetService("ServerStorage")
copyColors.Name = character.Name
oldColors:remove()
local newColors = Instance.new("BodyColors", character)
newColors.HeadColor = BrickColor.new("Dark green")
newColors.TorsoColor = BrickColor.new("Brown")
newColors.RightArmColor = BrickColor.new("Dark green")
newColors.LeftArmColor = BrickColor.new("Dark green")
newColors.RightLegColor = BrickColor.new("Brown")
newColors.LeftLegColor = BrickColor.new("Brown")
local parts = character:children()
local connections = {}
local zombies = {}
for _, object in next, parts do
if object:IsA("BasePart") then
connections[object:GetFullName()] = {object.Touched:Connect(function(touch)
local hum = touch.Parent:FindFirstChildWhichIsA("Humanoid")
if hum and hum.Health > 0 and not hum:FindFirstChild("isZombie") then
local zombie = zombify(touch.Parent)
zombies[touch.Parent:GetFullName() .. #zombies + 1] = zombie
end end), character}
if object.Name == "Head" then
local face = object:WaitForChild("face")
face.Texture = "http://www.roblox.com/asset/?id=7074882"
end 
end 
end
return {
["getConnections"] = function() return {unpack(connections)} end;
["disconnectConnections"] = function()
for i, connection in next, connections do
print(i)
if connection[1] ~= nil then
connection[1]:Disconnect()
local c = connection[2]
if not c then
return
end
local ch = c:WaitForChild("Humanoid")
if ch.Health > 0 then
local isCHzombie = ch:WaitForChild("isZombie")
if not isCHzombie then
return
end
isCHzombie:remove()
local colors = game:GetService("ServerStorage"):WaitForChild(c.Name)
local currColors = c:WaitForChild("Body Colors")
colors.Name = "Body Colors"
colors.Parent = c
currColors:remove()
connection = nil
end end end
for _, zombie in next, zombies do
zombie:disconnectConnections()
end
connections = nil
end
}
end

Please, for your own sake, make the code readable!

fixed it on my own:

function zombify(character)
local player = game:GetService("Players"):GetPlayerFromCharacter(character)
if player then
player.Dead.Value = true
end
if not character then
return
end
local zh = character:WaitForChild("Humanoid")
local isZombie = Instance.new("BoolValue", zh)
isZombie.Name = "isZombie"
isZombie.Value = true
local oldColors = character:WaitForChild("Body Colors")
local copyColors = oldColors:clone()
copyColors.Parent = game:GetService("ServerStorage")
copyColors.Name = character.Name
oldColors:remove()
local newColors = Instance.new("BodyColors", character)
newColors.HeadColor = BrickColor.new("Dark green")
newColors.TorsoColor = BrickColor.new("Brown")
newColors.RightArmColor = BrickColor.new("Dark green")
newColors.LeftArmColor = BrickColor.new("Dark green")
newColors.RightLegColor = BrickColor.new("Brown")
newColors.LeftLegColor = BrickColor.new("Brown")
local previousFaceTexture
local parts = character:children()
local disconnected = false
local connections = {}
local zombies = {}
for _, part in next, parts do
if part:IsA("BasePart") then
connections[part.Name] = {part.Touched:Connect(function(touch)
if disconnected and connections[part.Name] ~= nil then
connections[part.Name][1]:Disconnect()
connections[part.Name] = nil
return
end
local hum = touch.Parent:FindFirstChildWhichIsA("Humanoid")
if hum and hum.Health > 0 and not hum:FindFirstChild("isZombie") then
local iz = Instance.new("BoolValue", hum)
iz.Name = "isZombie"
iz.Value = true
zombies[touch.Parent] = {
["Character"] = touch.Parent;
["LastFaceTexture"] = touch.Parent.Head.face.Texture;
["OldColors"] = touch.Parent["Body Colors"]:clone()
}
zombies[touch.Parent].OldColors.Parent = game:GetService("ServerStorage")
local newZombie = zombify(touch.Parent)
zombies[touch.Parent].ZombieFunctions = newZombie
end end), character}
if part.Name == "Head" then
local face = part:WaitForChild("face")
previousFaceTexture = face.Texture
face.Texture = "http://www.roblox.com/asset/?id=7074882"
end end end
return {
["getConnections"] = function()
return {unpack(connections)}
end;
["disconnectConnections"] = function()
disconnected = true
for _, connection in next, connections do
if connection[1] ~= nil then
connection[1]:Disconnect()
end 
local c = connection[2]
if not c then
return
end
local hum = c:FindFirstChildWhichIsA("Humanoid")
if not hum then
return
end
local zombieTag = hum:FindFirstChild("isZombie")
if zombieTag then
zombieTag:remove()
end end
local head = character:WaitForChild("Head")
local face = head:WaitForChild("face")
face.Texture = previousFaceTexture
local colors = game:GetService("ServerStorage"):WaitForChild(character.Name)
local currColors = character:WaitForChild("Body Colors")
colors.Name = "Body Colors"
colors.Parent = character
currColors:remove()
end;
["disconnectChildZombies"] = function()
for _, zombie in next, zombies do
zombie.ZombieFunctions:disconnectChildZombies()
local zombieC = zombie.Character
if not zombieC then
return
end
local lastTexture = zombie.LastFaceTexture
local previuosColors = zombie.OldColors
local currColors = zombieC:WaitForChild("Body Colors")
previuosColors.Name = "Body Colors"
previuosColors.Parent = zombieC
currColors:remove()
local hum = zombieC:FindFirstChildWhichIsA("Humanoid")
if not hum then
return
end
local zombieTag = hum:FindFirstChild("isZombie")
if not zombieTag then
return
end
zombieTag:remove()
zombie.ZombieFunctions:disconnectConnections()
end end
}
end