on-the-fly hook.Add("PopulateToolMenu", ...)

-- for example,
local function populate_func()
	spawnmenu.AddToolMenuOption("Options", ...) -- your code
hook.Add("PopulateToolMenu", "dumbpopulate", populate_func)

-- but.. what if menu already populated?
-- If it's too late to populate, we can do /something/ tricky

-- epic hax by Sanya_Zol to make PopulateToolMenu work after gamemode loaded
-- took more than 2 hours reading standart lua files & experimenting to guess HOW TO F<3<3<3IN DO IT
if g_SpawnMenu then
	populate_func(); -- call your fresh hook
	local toolnum = nil
	for k,v in pairs( spawnmenu.GetTools() ) do
		if v.Name == "Options" then toolnum = k break end
		-- will not work if "Options" menu isn't exists
	if toolnum then
		-- it loads only ONE last element from spawnmenu.GetToolMenu("Options")
		local lastTable = spawnmenu.GetToolMenu("Options")[#spawnmenu.GetToolMenu("Options")]
		g_SpawnMenu:GetToolMenu():GetToolPanel( toolnum ):LoadToolsFromTable( {lastTable} )
-- if that's better way to do it - i'm an idiot

Can you give practical example usage of this?

I’m making admin-only script loader so admins can load their ESP’s without having to get VAC banned.

Why not load it directly instead of through AddToolMenuOption?
They wouldn’t get vac banned for lua.
They would only get gac banned if they bypassed allowcslua.

I mean banned for loading cheat on script enforced-server
For that script ability to load dynamically is critical

-snip- dumb
I still don’t get why you would need something like this. I can think of many not so hacky ways to let only specified people run scripts.

What does that have to do with the thread title or code you posted?

sendlua a cheat when they type a certain command and do a IsAdmin() check

[editline]15th October 2013[/editline]

I think she’s trying to use the hook to load their own lua cheats now that i think about it

I’d still just stick to a command that loads some generic esp, and then does

[lua]SendLua(“http.Fetch(‘link to your cheat’, RunString, function(err) end)”)[/lua]

thanks, i know how to run lua code.

but if cheat is loaded AFTER sandbox menu have been created, it will not be able to add something to tools menu.

hook.Add("PopulateToolMenu", ...)

will have no effect: that hooks already called by sandbox gamemode. if you try to reload all menus by calling CreateSpawnMenu

-- gamemodes\sandbox\gamemode\spawnmenu\spawnmenu.lua

   Called to create the spawn menu..
local function CreateSpawnMenu()

	-- If we have an old spawn menu remove it.
	if ( g_SpawnMenu ) then
		g_SpawnMenu = nil
	-- Start Fresh
	-- Add defaults for the gamemode. In sandbox these defaults
	-- are the Main/Postprocessing/Options tabs.
	-- They're added first in sandbox so they're always first
	hook.Run( "AddGamemodeToolMenuTabs" )
	-- Use this hook to add your custom tools
	-- This ensures that the default tabs are always
	-- first.
	hook.Run( "AddToolMenuTabs" )
	-- Use this hook to add your custom tools
	-- We add the gamemode tool menu categories first
	-- to ensure they're always at the top.
	hook.Run( "AddGamemodeToolMenuCategories" )
	hook.Run( "AddToolMenuCategories" )
	-- Add the tabs to the tool menu before trying
	-- to populate them with tools.
	hook.Run( "PopulateToolMenu" )

	g_SpawnMenu = vgui.Create( "SpawnMenu" )
	g_SpawnMenu:SetVisible( false )

	hook.Run( "PostReloadToolsMenu" )


-- ...

-- Hook to create the spawnmenu at the appropriate time (when all sents and sweps are loaded)
hook.Add( "OnGamemodeLoaded", "CreateSpawnMenu", CreateSpawnMenu )

it will raise tons of lua errors.

And that’s a way to add additional items to tools menu without breaking everything.

[editline]15th October 2013[/editline]

he’s <_<

Yeah, but

In the context of “lets my admins load esps” this makes no sense, because why would any hack want to add something to the tool menu?