--//Arms\\--

owner=script.Parent.Parent.Name
local cantouch

function onTouched(part)
	if part.Parent ~= nil then
		local h = part.Parent:findFirstChild("Humanoid")
		if h and h.Name~="Zombie" and h.Parent:findFirstChild("Torso") then
			h.Name="Zombie"
			if not game.Players:GetPlayerFromCharacter(h.Parent) then
				h.Parent.Name="Zombie"
			end
			if h.Parent.Name~=owner then
				if h.Parent:findFirstChild("zarm")~=nil then return end
				cantouch=0
				local larm=h.Parent:findFirstChild("Left Arm")
				local rarm=h.Parent:findFirstChild("Right Arm")
				if larm~=nil then
					larm:remove()
				end
				if rarm~=nil then
					rarm:remove()
				end
				local zee=Instance.new("Part", workspace)
				zee.Size=Vector3.new(1,2,1)
				zee.Color=Color3.fromRGB(161, 196, 140)
				zee.TopSurface=Enum.SurfaceType.Smooth
				zee.BottomSurface=Enum.SurfaceType.Smooth
				zee.Name="Left Arm"
				if zee~=nil then
					local zlarm=zee
					local zrarm=zee:clone()
					zrarm.Name="Right Arm"
					if zlarm~=nil then
						local rot=CFrame.new(0, 0, 0, 0, 0, 1, 0, 1, 0, -1, 0, 0)*CFrame.Angles(1.6,0,1.5)
						zlarm.CFrame=h.Parent.Torso.CFrame * CFrame.new(Vector3.new(-1.5,0.5,-0.5)) * rot
						zrarm.CFrame=h.Parent.Torso.CFrame * CFrame.new(Vector3.new(1.5,0.5,-0.5)) * rot
						zlarm.Parent=h.Parent
						zrarm.Parent=h.Parent
						local Ef=Instance.new("WeldConstraint",	zrarm)
						Ef.Enabled=false
						Ef.Part0=zrarm
						Ef.Part1=h.Parent.Torso
						Ef.Enabled=true
						Ef=Instance.new("WeldConstraint", zlarm)
						Ef.Enabled=false
						Ef.Part0=zlarm
						Ef.Part1=h.Parent.Torso
						Ef.Enabled=true
						Ef=nil
						zlarm.Anchored=false
						zrarm.Anchored=false
						for i,v in pairs(h.Parent:GetDescendants()) do
							if v:IsA("Script") then
								v.Disabled=true
								v:Destroy()
							elseif v:IsA("Tool") or v:IsA("BodyPosition") or v:IsA("BodyGyro") then
								v:Destroy()
							end
							local ZombieAI=script:Clone()
							ZombieAI.Disabled=true
							ZombieAI.Parent=h.Parent
							delay(0.2, function()
								ZombieAI.Disabled=false
							end)
						end
						wait(0.1)
						h.Parent.Head.Color=zee.Color
					else return end
				end
				wait(1)
				cantouch=1
			end
		end
	end
end

do -- Set Arms --
	do
		local c=script.Parent:GetChildren()
		for i=1,#c do
			if c[i].Name:lower():find("arm") then
				c[i].Touched:Connect(onTouched)
			end
		end
	end
	f=script.Parent.ChildAdded:Connect(function(v)
		if v.Name:lower():find("arm") then
			v.Touched:Connect(onTouched)
		end
	end)
	task.delay(1,function()
		if f then f:Disconnect() end
	end)
end


if not Player then
	if script.Parent.Config:WaitForChild("AntiExploit").Value==true then
		script.Parent:FindFirstChildOfClass("Part"):SetNetworkOwner(nil)
	end
	Sight=script.Parent.Config:WaitForChild("Sight")

	garbage={}
	do
		local stuff={}
		garbage.add=function(...)
			local stuf:any={...}
			for i=1,#stuf do
				table.insert(stuff,stuf[i])
			end
			table.clear(stuf)
			stuf=nil
		end
		garbage.nuke=function()
			for i=1,#stuff do
				local v=stuff[i]
				if typeof(v)=="RBXScriptConnection" then
					v:Disconnect()
					stuff[i]=nil
				elseif typeof(v)=="Instance" then
					pcall(game.Destroy,v)
					stuff[i]=nil
				elseif typeof(v)=="thread" then
					pcall(coroutine.yield,v)
					task.delay(5,function()
						pcall(coroutine.close,v)
						stuff[i]=nil
					end)
				elseif typeof(v)=="function" then
					v()
					stuff[i]=nil
				end
				v=nil
			end

		end
	end
	-- Better Model-list, Capable of finding ALL humanoid-models and removes them from memory on remove
	-- Greatly improved.
	local list
	do -- Creates a new Scope for the Humanoid List, to not clutter the global space.
		list={}
		local function parse_instance(inst) -- Parses an Instance to be set in the model.
			if inst:IsA("Humanoid") then -- Only goes through if it's a Humanoid.
				local model=inst.Parent -- Grabs the model.
				if model~=workspace then  -- Assures it isnt grabbing the workspace.
					table.insert(list,model) -- Inserts it into the list.
					local Connector -- Predefines Connector so it can be Disconnected.
					Connector=model.AncestryChanged:Connect(function() -- Created a Connection on AncestryChanged, so if the Humanoids ancestry is removed from Workspace, it nolonger see's them.
						if not model:IsDescendantOf(workspace) then -- Checks if not descendant of workspace.
							table.remove(list,table.find(model)) -- Removes the model from the list
							model,inst=nil,nil -- Clears all memory out, allowing garbage collection.
							Connector:Disconnect() -- Disconnects connector forever.
							Connector=nil; -- Finally removes Connector.
						end
					end)
				end
			end
		end

		do 
			local a=workspace:GetDescendants() -- Gets all workspace descendants
			for i=1,#a do -- Uses a Numeric loop, since it's fastest.
				parse_instance(a[i])
			end
			table.clear(a)
			a=nil
		end
		garbage.add(function()
			list=nil
		end,workspace.DescendantAdded:Connect(parse_instance))
			--[[
	Unlike most methods of obtaining humanoids, this can safely obtain ALL humanoids without significant lag,
	most other methods involve looping through all workspace children, or descendants!
	This only loops once on startup, and relies on DescendantAdded thus resulting in less calls and if-statements.
	--]]
	end

	--//Functions\\--
	function FindNearestHumanoid() -- Circles through the "List" shown just above the function.
		local Closest=nil
		local ClosestBP=nil
		for i,v in pairs(list) do
			if v.Name=="Zombie" then --Removes players and zombies from list entirely.
				table.remove(list,i)
			end
			local PHumanoid = v:FindFirstChildOfClass("Humanoid")
			if PHumanoid and PHumanoid.Name~=Humanoid.Name then

				if not Closest or not ClosestBP then
					Closest=v
					ClosestBP=v:FindFirstChildOfClass("Part")
				elseif Closest and ClosestBP then
					local MaybeClosestsBP=v:FindFirstChildOfClass("Part")
					if MaybeClosestsBP and MaybeClosestsBP:IsA("BasePart") then
						if (ClosestBP.Position-larm.Position).Magnitude>(MaybeClosestsBP.Position-larm.Position).Magnitude then
							Closest=v
							ClosestBP=v:FindFirstChildOfClass("Part")
						end
					end
				end

			end
		end
		return Closest,ClosestBP
	end
	--//Alive\\--
	Alive=true
	garbage.add(Humanoid.Died:Connect(function()
		Alive=false
	end),Humanoid.AncestryChanged:Connect(function()
		if not Humanoid:IsDescendantOf(workspace) then
			Alive=false
		end
	end),script.Parent.ChildRemoved:Connect(function(E)
		if E==larm then
			larm=script.Parent:FindFirstChildOfClass("Part")
			if not larm and Humanoid then
				Humanoid.Health=0
				Alive=false
			else
				script.Parent:Destroy()
			end
		end
	end))

	while Alive do
		if larm then
			local Model,Part=FindNearestHumanoid()			
			if Model and Part and (Part.Position-larm.Position).Magnitude<Sight.Value then
				Humanoid:MoveTo(Part.Position,Part)
			else
				Humanoid:MoveTo(larm.Position+Vector3.new(math.random(-20,20),math.random(-20,20),math.random(-20,20)))
			end 
		end
		task.wait(math.random(1,10))
	end

	do
		local c=script.Parent:GetChildren() 
		for i=1,#c do
			if c[i]~=script then
				c[i]:Destroy()
			end
		end
	end
	wait(5)
	script.Parent:Destroy()
end
