How do I make a tool that executes a certain command when used?

I know this is a dumb question, but I’ve never coded a STool before. Essentially, I want it to, when used, execute a command on the player using it. A command they don’t have access to, that is. Ultimately, I want the user to select the STool, left click, and have the command rp_cp <user> run on them. And keep in mind, I don’t want them to be able to use this on others. How would I make this?

Once again, I apologize if this is a dumb request, but any help would really be appreciated.

http://wiki.garrysmod.com/?title=SWEP (AdminSpawnable = true, spawnable = false)

Does that help?

[lua]if SERVER then
AddCSLuaFile(“weapons/gmod_stool/stools/permaprops.lua”)
end

TOOL.Name = “#PermaProps
TOOL.Category = “Construction”
TOOL.Command = nil
TOOL.ConfigName = “”

function TOOL:LeftClick( tr )
RunConsoleCommand(“perma_save”)
return true
end

function TOOL:RightClick( tr )
RunConsoleCommand(“perma_remove”)
return true
end

function TOOL:Reload( tr )
RunConsoleCommand(“perma_save”,“1”)
return true
end

function TOOL.BuildCPanel( panel )

panel:AddControl( "Header"  , { Text = "#Tool_permaprops_name", Description	= "#Tool_permaprops_desc" }  )

end[/lua]

A basic tool I experimented with recently that does what you’re asking.

That’s very helpful, thank you very much! I’m having some difficulty with making it, however. I believe I’m underqualified to make a STool right now, and I’ll just look for a lua coder to help me with it. After trying for an hour, I can’t get it to trace, or even get the correct name to appear near the top of the screen. Thank you very much for your help, though.

EDIT: Sorry, Entoros, didn’t even see your post. That certainly points me in the right direction, and I’ll work from that, but I still can’t get the #Tool_vipjob_name (what I named it), to say what I want it to say. Also, the RunConsoleCommand executes the command from my perspective, sure, but I’m not sure it would work for someone that doesn’t have the privileges to use the command I want them to use. I want the console to use this command ON the user who’s using the tool, but I’m not sure how to make it do that.

I think the names like #Tool_<tool>_name are dependent on the name of the file, possibly. Make sure you name the file vipjob.lua.

And to get the entity the player is looking at, use the trace argument passed to the TOOL:Click functions

[lua]function TOOL:LeftClick( tr )
print( tr.Entity:IsPlayer() and “It’s a player!” or “Not a player…” )
– concommand would look something like
tr.Entity:ConCommand(“say har har I got tooled”)
end[/lua]

I don’t want the command to be done on the entity the player is looking at, I want it to be used on the player. Keep in mind, if the player does the command in console, it won’t work. I want the tool to do the command from console (as if it were rcon) on the user that has the tool. How might I do that?

You can use **[Game.ConsoleCommand

http://wiki.garrysmod.com/favicon.ico](http://wiki.garrysmod.com/?title=Game.ConsoleCommand)** to run a command from the server, if that’s what you’re asking.

Yes, I’ve looked at that command, however, won’t it just run the command, without knowing which user to run it on? I want this tool to work on the person currently using it, not a specific person. The command I want to run is something like “rp_cp <user using the tool>”. If I run it from the user, they won’t have the privileges, but if I run it from console, I can’t specify who I want it to be used on (without your help). Is there some way for it to detect which person is using the specific tool, and essentially “fill in” their name in that command? Like, if I’m using it, I want the command it runs to be rp_cp Prizim. If my hypothetical friend John uses it, I want to be rp_cp John. Is that possible? Sorry if I’m being picky/dumb.

Instead of Game.ConsoleCommand

self.Owner:ConCommand("rp_cp "…self.Owner:Nick())

I suppose

Willox is referring to **[Player.ConCommand

http://wiki.garrysmod.com/favicon.ico](http://wiki.garrysmod.com/?title=Player.ConCommand)** if you want the link to the Wiki, which is always helpful. The wiki is always your friend.

If it needs server-level authentication to run, then what you do is you pass a unique component of the player you want to use it on, such as their steamID, uniqueID, or entity number. i.e.

[lua]concommand.Add(“do_things”,function(_,cmd,args)
local id = tonumber(args[1])
for _,pl in pairs(player.GetAll()) do
if pl:UniqueID() == id then
– then we do things to the player, pl
end
end
end)[/lua]

I’m sorry, I must sound like a complete moron (I might be one), but I am completely lost here. I don’t know what to edit/write/change to make this work. Sorry if I’m wasting your time.

Ok. I might have strayed a little off topic, after reading your previous posts and better understanding what the topic is. I think this is what you want:

[lua]function TOOL:LeftClick( tr )
if tr.Entity:IsPlayer() then
game.ConsoleCommand("rp_cp " … tr.Entity:Name());
end
end[/lua]

I’m really sorry to leave you hanging. I had to go on a fairly unexpected week long trip due to family business. I’m really sorry if I’m wasting your time, but I added it to my server, and two things happen:

  1. When I left click without targeting someone, it does nothing.
  2. When I target someone, it displays this error message, then does nothing:
    weapons\gmod_tool\stools\vipjob.lua:12: attempt to call field ‘ConsoleCommand’ (a nil value)

I’m sorry if I’m slow. I’m going to add you on Steam if you don’t mind and see if I can work this out outside of Facepunch.