How to make a simple wallhack

From GMod Wiki

(Difference between revisions)
Jump to: navigation, search
m (Added Tabbing)
 
Line 26: Line 26:
hook.Add( "HUDPaint", "Wallhack", function()
hook.Add( "HUDPaint", "Wallhack", function()
-
for k,v in pairs ( player.GetAll() ) do
+
for k,v in pairs ( player.GetAll() ) do
-
-- This is a loop going across all players.
+
-- This is a loop going across all players.
-
end
+
end
end )
end )
Line 46: Line 46:
hook.Add( "HUDPaint", "Wallhack", function()
hook.Add( "HUDPaint", "Wallhack", function()
   
   
-
for k,v in pairs ( player.GetAll() ) do
+
for k,v in pairs ( player.GetAll() ) do
   
   
-
local Position = ( v:GetPos() + Vector( 0,0,80 ) ):ToScreen()
+
local Position = ( v:GetPos() + Vector( 0,0,80 ) ):ToScreen()
-
draw.DrawText( v:Name(), "MenuLarge", Position.x, Position.y, Color( 255, 255, 255, 255 ), 1 )
+
draw.DrawText( v:Name(), "MenuLarge", Position.x, Position.y, Color( 255, 255, 255, 255 ), 1 )
   
   
-
end
+
end
   
   
end )
end )
Line 71: Line 71:
hook.Add( "HUDPaint", "Wallhack", function()
hook.Add( "HUDPaint", "Wallhack", function()
-
if ConVarExists( "wallhack_enabled" ) and GetConVar("wallhack_enabled"):GetInt() == 1 then
+
if ConVarExists( "wallhack_enabled" ) and GetConVar("wallhack_enabled"):GetInt() == 1 then
   
   
-
for k,v in pairs ( player.GetAll() ) do
+
for k,v in pairs ( player.GetAll() ) do
   
   
-
local Position = ( v:GetPos() + Vector( 0,0,80 ) ):ToScreen()
+
local Position = ( v:GetPos() + Vector( 0,0,80 ) ):ToScreen()
-
draw.DrawText( v:Name(), "MenuLarge", Position.x, Position.y, Color( 255, 255, 255, 255 ), 1 )
+
draw.DrawText( v:Name(), "MenuLarge", Position.x, Position.y, Color( 255, 255, 255, 255 ), 1 )
   
   
-
end
+
end
-
end
+
end
   
   
end )
end )
Line 103: Line 103:
hook.Add( "HUDPaint", "Wallhack", function()
hook.Add( "HUDPaint", "Wallhack", function()
   
   
-
for k,v in pairs ( player.GetAll() ) do
+
for k,v in pairs ( player.GetAll() ) do
-
local Position = ( v:GetPos() + Vector( 0,0,80 ) ):ToScreen()
+
local Position = ( v:GetPos() + Vector( 0,0,80 ) ):ToScreen()
-
local Name = ""
+
local Name = ""
-
if v == LocalPlayer() then Name = "" else Name = v:Name() end
+
if v == LocalPlayer() then Name = "" else Name = v:Name() end
-
draw.DrawText( Name, "MenuLarge", Position.x, Position.y, Color( 255, 255, 255, 255 ), 1 )
+
draw.DrawText( Name, "MenuLarge", Position.x, Position.y, Color( 255, 255, 255, 255 ), 1 )
   
   
-
end
+
end
   
   
end )
end )

Latest revision as of 13:07, 28 September 2011

Lua: How to make a simple wallhack
Page white text.png Description:A tutorial on how to make a simple wallhack.
link=User:Assault_Trooper Original Author:Assault_Trooper
Calendar.png Created:October 24th 2010

In this tutorial, you'll learn how to make a simple wallhack. And naturally, only for learning purposes.

Contents

Getting started

We'll start by creating the lua file, just create "Wallhack.lua" to the lua directory of Garry's Mod. And when you want to test it, just type lua_openscript_cl Wallhack.lua

Wallhack tutorial

Okay, so lets get started. This is the main part, it will make a hook, which will draw our text to the screen.

 
hook.Add( "HUDPaint", "Wallhack", function()
 
 
end )
 

But of course, that's not the wallhack itself, we'll still need to add something to it. Because it's an wallhack, we'll have to go through the players of course.

 
hook.Add( "HUDPaint", "Wallhack", function()
 
	for k,v in pairs ( player.GetAll() ) do
 
		-- This is a loop going across all players.
 
	end
 
end )
 

Now that we have got all the players, we just need the text above their head. With this we are getting the players position, and converting it into a position on our screen. The vector addition is because we don't want to have the name on the players feet, do we.

 
local Position = ( v:GetPos() + Vector( 0,0,80 ) ):ToScreen()
 

Now we'll just need to implement this into the script you made. This is the most important function on this, it will draw the players name above his head. I'll explain about the function, The first argument is the text to draw, in this case it would be the players. The second argument is the font, feel free to find a suitable for you from the Working with Fonts page. The third and the fourth are the texts position X, and Y. The fifth is the color, feel free to change it. The sixth and the last one is just the Xalign, you can just leave it. More info about this function on the draw.DrawText page.

 
draw.DrawText( v:Name(), "MenuLarge", Position.x, Position.y, Color( 255, 255, 255, 255 ), 1 )
 

Now you just have to add these two new functions to the loop and you're done!

 
hook.Add( "HUDPaint", "Wallhack", function()
 
	for k,v in pairs ( player.GetAll() ) do
 
		local Position = ( v:GetPos() + Vector( 0,0,80 ) ):ToScreen()
		draw.DrawText( v:Name(), "MenuLarge", Position.x, Position.y, Color( 255, 255, 255, 255 ), 1 )
 
	end
 
end )
 

Optional additions

You might end up wanting to have a on/off switch for the wallhack, or you may have noticed that it draws the name for you also. I'll show you examples how to do some of these.

On/Off switch

First, I'll show you how to make a on/off switch. The first argument is the convar name, feel free to change it. The second argument is the default value, 0 is disabled, and 1 is enabled. The third argument is an option that should it save, we'll but yes. The fourth one is an option that is it userdata, for us, no. More info about this on CreateClientConVar page.

 
CreateClientConVar( "wallhack_enabled", 0, true, false )
 

Now that we have the ConVar, we'll have to find a way how to use it. The best way would be to have it in the hook.

 
hook.Add( "HUDPaint", "Wallhack", function()
 
	if ConVarExists( "wallhack_enabled" ) and GetConVar("wallhack_enabled"):GetInt() == 1 then
 
		for k,v in pairs ( player.GetAll() ) do
 
		local Position = ( v:GetPos() + Vector( 0,0,80 ) ):ToScreen()
		draw.DrawText( v:Name(), "MenuLarge", Position.x, Position.y, Color( 255, 255, 255, 255 ), 1 )
 
		end
 
	end
 
end )
 
 

Now what did I do there? I first checked does the convar exist, and then I'm using the function GetConVar to get the value of the ConVar we created, and because it would only return the convar itself, I added GetInt() to get the value.

And now you're done! You only have to type wallhack_enabled 1 in console to enable it, and wallhack_enabled 0 to disable it.

Removing your name

Isn't it annoying that if you look on the ground or the sky, you might see your own name, I'll show you how to fix that.

 
local Name = ""
 
if v == LocalPlayer() then Name = "" else Name = v:Name() end
 

This is how you'll make it detect that, if the target is you, then "Name" is empty, else "Name" is the players name.

 
hook.Add( "HUDPaint", "Wallhack", function()
 
	for k,v in pairs ( player.GetAll() ) do
 
		local Position = ( v:GetPos() + Vector( 0,0,80 ) ):ToScreen()
		local Name = ""
 
		if v == LocalPlayer() then Name = "" else Name = v:Name() end
 
		draw.DrawText( Name, "MenuLarge", Position.x, Position.y, Color( 255, 255, 255, 255 ), 1 )
 
	end
 
end )
 

And now you're done! Now you should be able to combine these two if you want by yourself. And hopefully, you learned something about making a simple wallhack.

What it will look like

th.259cb4wallhackexample.png Here is an example image of the wallhack.

Personal tools
Namespaces
Variants
Actions
Navigation
Lua Scripting
Functions
Hooks
Toolbox