Skip to content

Lua Examples

Various examples of scripts

Warning

A few GUI related examples need to be redone, due to removal of gui.Tab


Draw Time Since Cheat Injection

local function ExampleDrawingHook()
    draw.Color(220, 50, 50, 255)
    draw.Text(128, 128, tostring(common.Time()))
end

callbacks.Register("Draw", "ExampleDrawingHook", ExampleDrawingHook)

Draw Text With Custom Font

draw.AddFontResource("location/to/ttf/file.ttf")
local Font = draw.CreateFont("ttfname", 20, 100)

callbacks.Register("Draw", function()
    draw.SetFont(Font)

    draw.Color(255, 138, 130, 255)
    draw.Text(60, 60, "Text")
end)

Input Monitoring

if input.IsButtonDown(1) then
    print("Left click is down!")
end

if input.IsButtonPressed(2) then
    print("Right click is pressed!")
end

if input.IsButtonReleased(1) then
    print("Left click is released!")
end

Searching Memory Patterns

local address = mem.FindPattern("client.dll", "C3 ?? CC")

SVG Drawing

local svgData = http.Get("https://upload.wikimedia.org/wikipedia/commons/f/fd/Ghostscript_Tiger.svg")
local imgRGBA, imgWidth, imgHeight = common.RasterizeSVG(svgData)
local texture = draw.CreateTexture(imgRGBA, imgWidth, imgHeight)

callbacks.Register("Draw", function()
    draw.SetTexture(texture)
    draw.FilledRect(0, 0, imgWidth, imgHeight)
end)


Get Text Size

callbacks.Register("Draw", function()
    local Tw, Th = draw.GetTextSize("The size of this text")

    print("Text Width - " .. Tw .. " Text Height - " .. Th)

    --Text Width - 123 Text Height - 9
end)

Scissor Method

callbacks.Register("Draw", function()
    draw.SetScissorRect(300,500,300,300)

    draw.Color(0,0,0,255)
    draw.FilledRect(300, 500, 600, 800)

    draw.Color(255,255,255,255)
    draw.FilledRect(300, 400, 400, 800)

    --Reset scissor area to allow seeing menu.
    draw.SetScissorRect(0, 0, draw.GetScreenSize())
end)

Custom Window GUI

local custom_window = gui.Window("custom_window", "Lua Window", 200, 200, 200, 400)
local custom_groupbox = gui.Groupbox(custom_window, "Groupbox", 16, 16)

-- Creating the object with a function embeded
local ebtn_button = gui.Button(custom_groupbox, "Clicky Clicky! #1", function()
    print("you clicked me! Yay! #1")
end)

Using GUI Colorpicker Values

local ecp_misc = gui.Reference("MISC")
local ecp_gui = gui.Tab(ecp_misc, "ecp_gui", "Example Color Picker")

-- Creating the object
local ecp_colorpicker = gui.ColorPicker(ecp_gui, "ecp_colorpicker", "Pick your color!", 255, 0, 0, 255)

-- Return Example
local r, g, b, a = ecp_colorpicker:GetValue()

-- Setting ColorPicker value
ecp_colorpicker:SetValue({255, 255, 255, 255})

-- Passing into Function Examples
callbacksRegister("Draw", function()
    Colors_A(ecp_colorpicker)
    Colors_B(ecp_colorpicker:GetValue())
end)

local function Colors_A(color)
    local r, g, b, a = color:GetValue()
    print("Colors_A - "  "Red: "  r  " Green: "  g  " Blue: "  b  " Alpha: "  a)
end


local function Colors_B(r, g, b, a)
    print("Colors_B - "  "Red: "  r  " Green: "  g  " Blue: "  b  " Alpha: "  a)
end

Listbox GUI

local elb_misc = gui.Reference("MISC")
local elb_gui = gui.Tab(elb_misc, "ecb_gui", "Example Listbox")

-- Creating the object with a single item
local elb_listbox_a = gui.Listbox(elb_gui, "elb_listbox_a", 200, "Item 1")

-- Creating the object with multiple item(s)
local elb_items = {"Item1", "Item2", "Item3", "Item4"}
local elb_listbox_b = gui.Listbox(elb_gui, "elb_listbox_b", 200, unpack(elb_items))

-- Return selected value
callbacks.Register("Draw", function()
    local elb_value_a = elb_listbox_a:GetValue()
    local elb_value_b = elb_listbox_b:GetValue()
    print("elb_value_a: " .. elb_value_a)
    print("elb_value_b: " .. elb_value_b)
end)

Slider GUI

local esdr_misc = gui.Reference("MISC")
local esdr_gui = gui.Tab(esdr_misc, "esdr_gui", "Example Slider")

-- Creating the object with a function embeded
local esdr_slider = gui.Slider(esdr_gui, "esdr_slider", "My happy little slider", 5.0, 0.0, 10.0, 0.01)

-- Return slider value
callbacks.Register("Draw", function()
    local esdr_value = esdr_slider:GetValue()
    print("esdr_value: " .. esdr_value)
end)

-- Set slider value
esdr_slider:SetValue(0.172)

GUI Including XML

<Window var="mymenu" name="My Menu" width="300" height="300">
    <Tab name="Tab 1">
        <Groupbox name="Options">
            <Checkbox var="enable" name="Enable" value="on">
                <ColorPicker var="clr" value="240 30 20 255"/>
            </Checkbox>
            <Slider var="factor" name="Factor" desc="Determine the factor." min="0" max="100"/>
            <Combobox var="mode" name="Mode" options=["Offensive","Defensive"]/>
        </Groupbox>
    </Tab>
    <Tab name="Tab 2">
        <Text>This tab is empty</Text>
    </Tab>
</Window>
local test = gui.XML(file.Read("test.xml"))
local enable = test:Reference("enable")
local color = enable:Reference("clr")
local factor = test:Reference("factor")
local mode = test:Reference("mode")

callbacks.Register("Draw", function()
    if enable:GetValue() then
        draw.Color(color:GetValue())
        draw.Text(16, 16, "Enabled with factor " .. factor:GetValue() .. " and mode " .. mode:GetString())
    end
end)

Draw Text On Enabled Checkbox

local debugFont = draw.CreateFont("Tahoma", 60)
local function OnDraw()
    draw.SetFont(debugFont)
    if lua_checkbox:GetValue() then
        draw.Text(200, 200, "Checkbox is checked")
    end
    draw.Text(200, 300, "Slider: " .. lua_slider:GetValue())
end

callbacks.Register("Draw", "LuaGuiTest", OnDraw)

Script Load Via GUI

gui.Command("lua.load <scriptname>.lua")

Using GUI Methods

local Tab = gui.Tab(gui.Reference("Visuals"), "ScriptTabValue", "MyScript")
local Groupbox = gui.Groupbox(tab, "Groupbox", 16, 16)

local Value, Name = Groupbox:GetValue(), Groupbox:GetName()

Groupbox:SetDescription("New Description")
Groupbox:SetDisabled(true)
Groupbox:Remove()

Getting / Setting Config Values

local ragebotenabled = gui.GetValue("rbot.master")

gui.SetValue("rbot.master", true)

Note

To retrieve set config values, export your config to clipboard, and you will be able to see the values you can get/set


Reading Children Objects

local function PrintChildren(obj, prefix)
    print(prefix  obj:GetName())
    for child in obj:Children() do
        PrintChildren(child, prefix  "\t")
    end
end

PrintChildren(gui.Reference("MENU"), "")

File Writing

local f = file.Open("myfile.txt", "w")
f:Write("mydata")
f:Close()

Enumerating Files

file.Enumerate(function(fil)
    print(fil)
    --Prints list of files
end)

UDP Client And Server

local serverIP = "127.0.0.1"
local serverPort = 1234

local server = network.Socket("UDP")
local client = network.Socket("UDP")

if server:Bind(serverIP, serverPort) then
    print("Socket bound to port " .. serverPort)
end

local size = client:SendTo(serverIP, serverPort, "Hello!")

if size > 0 then
    print("Sent " .. size .. " bytes")
end

callbacks.Register("Draw", function()
    local msg, ip, port = server:RecvFrom("0.0.0.0", 0, 100)

    if msg then
        print("Received message from " .. ip .. ":" .. port .. ": " .. msg)
    end
end)

Registering and Unregistering Callbacks

callbacks.Register("Draw", "Unique", function()
    print("Draw Callback")
end)

callbacks.Register("Draw", function()
    print("Draw Callback")
end)

callbacks.Register("Unload", function()
    print("The script unloaded!")
end)

callbacks.Unregister("Draw", "Unique")