Skip to content

CS2 Lua Examples

Various CS2 specific usage examples

Snap Lines ESP

local function ExampleSnapLines()
    local screenCenterX, screenH = draw.GetScreenSize()
    screenCenterX = screenCenterX * 0.5

    draw.Color(255, 0, 0, 255)

    local players = entities.FindByClass("CCSPlayer")

    for i = 1, #players do
        local player = players[i]

        if player:IsAlive() then
            local x, y = client.WorldToScreen(player:GetAbsOrigin())

            if x ~= nil and y ~= nil then
                draw.Line(x, y, screenCenterX, screenH)
            end
        end
    end
end

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

Killsay

local Kill_String = "get owned"
local Death_String = "nice luck"

local function CHAT_KillSay(Event)

   if (Event:GetName() == "player_death") then

       local ME = client.GetLocalPlayerIndex()

       local INT_UID = Event:GetInt("userid")
       local INT_ATTACKER = Event:GetInt("attacker")

       local NAME_Victim = client.GetPlayerNameByUserID(INT_UID)
       local INDEX_Victim = client.GetPlayerIndexByUserID(INT_UID)

       local NAME_Attacker = client.GetPlayerNameByUserID(INT_ATTACKER)
       local INDEX_Attacker = client.GetPlayerIndexByUserID(INT_ATTACKER)

       if (INDEX_Attacker == ME and INDEX_Victim ~= ME) then

           client.ChatSay(" " .. tostring(Kill_String) .. " " .. NAME_Victim)

       elseif (INDEX_Victim == ME and INDEX_Attacker ~= ME) then

           client.ChatSay(" " .. tostring(Death_String) .. " " .. NAME_Attacker)

       end

   end

end

client.AllowListener("player_death")

callbacks.Register("FireGameEvent", "AWKS", CHAT_KillSay)

Auto Buy

local function autobuy(event)
   if event:GetName() == "round_prestart" then
       client.Command("buy scar20 buy deagle buy vest buy vesthelm buy incgrenade buy molotov buy hegrenade buy smokegrenade", true)
   end
end

client.AllowListener("round_prestart")

callbacks.Register("FireGameEvent", "autobuy", autobuy)

Radio Spammer

local last_spam  = globals.TickCount()

local function RadioSpam()
   if globals.TickCount() - last_spam > 32 then
       client.Command("getout")
       last_spam = globals.TickCount()
   end
end

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

Vector Math

Warning

This API is deprecated since V5.1 and removed in V6 products

Use Vector3 and EulerAngles classes

print(vector.Length(100, 100, 100))
print(vector.Add({0, 0, 0}, {100, 100, 100}))
print(vector.Add({0, 0, 0}, 100))
print(vector.Distance(0, 0, 0, 100, 100, 100))
print(vector.Distance({0, 0, 0 }, {100, 100, 100}))

local function OnDrawESP(builder)
    local ent = builder:GetEntity()
    local localply = entities.GetLocalPlayer()

    builder:AddTextTop("Distance: " .. vector.Distance({ent:GetAbsOrigin()}, {localply:GetAbsOrigin()}))
end

callbacks.Register("DrawESP", OnDrawESP)

Chat Message Replacement

callbacks.Register("SendStringCmd", function(cmd)
    -- replace "no" with "yes"
    if string.find(cmd:Get(), "say \"no\"") == 1 then
        cmd:Set("say \"yes\"")
    end
end)

Tracing Using TraceLine

local src = ...
local dst = ...

local tr = engine.TraceLine(src, dst, 0xFFFFFFFF)

print(tr.fraction)
print(tr.entity)
print(tr.normal)

Running Panorama JS

local value = 100

panorama.RunScript([[
    $.Msg("Loaded Panorama Script");

    NewValue = ]].. value ..[[;
    NewValue += 25;

    GameInterfaceAPI.ConsoleCommand(`r_eyemove "${NewValue}"`);
]]);

local JSValue = client.GetConVar("r_eyemove")
print(JSValue)

--125

Entity Examples

local Players = entities.FindByClass("CCSPlayerController")
local playerResources = entities.GetPlayerResources()
local RandomPlayer = entities.GetByIndex(3)
for i = 1, globals.MaxClients(), 1 do
    local Player = entities.GetByUserID(i)
end

Localplayer Example

local result = ""

local function CheckLocalPlayerStatus()
    local LocalPlayer = entities.GetLocalPlayer()
    if LocalPlayer:IsAlive() then
        result = "I'm alive"
    else
        result = "I'm dead"
    end

    print(result)
end

callbacks.Register("Draw", CheckLocalPlayerStatus)

Finding Material By Name

local material = materials.Find("particle/screenspace_fog")
print(material:GetTextureGroupName())

Enumerating Materials

materials.Enumerate(function(mat)
    print(mat)
    if string.find(mat:GetTextureGroupName(), "World") then
        mat:AlphaModulate(0.3)
    end
end)

Get Cmd Buttons

-- Add IN_ATTACK button to the current buttons.
cmd.buttons = cmd.buttons | (1 << 0)

Angles Usage Example

local targetDirection = localplayer:GetAbsOrigin() - entity:GetAbsOrigin()
local targetAngles = targetDirection:Angles()
cmd.viewangles = targetAngles

Vector3 Usage Example

local v1 = Vector3(100, 0, 0)
local v2 = Vector3(200, 0, 0)

print(v1:LengthSqr())
print(v2:Length2D())