I am new to lua, I made a code for a program that is supposed to help you in the console. As soon as I open it in gmod I get these errors:
[ERROR] lua/console_helperr.lua:4: function arguments expected near 'print'
1. unknown - lua/console_helperr.lua:0
in the console.
The code for the program is:
[CODE]chat.AddText( "Console Helper Initialized" );
concommand.Add("airboat",function(cmd)
cmd:ch_createairboat
print("airboat spawned");
end )
concommand.Add("car",function(cmd)
cmd:ch_createjeep
chat.AddText("jeep spawned");
end )
concommand.Add("car2",function(cmd)
cmd:ch_createjalopy
chat.AddText("jalopy spawned");
end )
concommand.Add( "viewids", function()
for _, v in pairs( player.GetAll() ) do
print( v:Nick() .. ", " .. v:SteamID() .. "\n" )
end
end )
concommand.Add("crazymode",function(ply)
ply:+attack
bug_swap
+attack
bug_swap
-attack
chat.AddText("crazymode complete");
end )
concommand.Add("ammo",function(ply)
ply:givecurrentammo()
chat.AddText("Ammo given from god");
end )[/CODE]
I am a noob so I have no idea how to fix it. I know its some kind of thing in line 4 on print.
please help!:quagmire:
'cmd:ch_createairboat'
tells gmod to run command ch_createairboat
That's not valid Lua
[QUOTE=Chessnut;46887910]That's not valid Lua[/QUOTE]
How not?
If it was valid Lua, you wouldn't have made this thread.
Where did you get the idea that cmd:<something> would work?
[QUOTE=Chessnut;46887948]If it was valid Lua, you wouldn't have made this thread.
Where did you get the idea that cmd:<something> would work?[/QUOTE]
Some gmod tutorials, should it be ply instead?
Give a link to the tutorial
[code]local vehicles = { car = "prop_vehicle_airbot", jeep = "prop_vehicle_jeep_old", jalopy = "prop_vehicle_jeep" }
chat.AddText( "Console Helper Initialized" )
concommand.Add( "spawn_vehicle", function( ply, cmd, args )
if ( vehicles[ cmd[ 1 ] ] ~= nil ) then
ents.Create( cmd[ 1 ] )
-- Other ent placement here
end
end )
concommand.Add( "viewids", function()
for _, v in pairs( player.GetAll() ) do
print( v:Nick() .. ", " .. v:SteamID() .. "\n" )
end
end )
concommand.Add( "crazymode", function( ply )
ply:ConCommand( "+attack" )
ply:ConCommand( "bug_swap" )
ply:ConCommand( "+attack" )
ply:ConCommand( "bug_swap" )
ply:ConCommand( "-attack" )
if CLIENT then chat.AddText( "Crazymode complete" ) end
end )
concommand.Add( "ammo", function( ply )
ply:GiveAmmo( 999, ply:GetActiveWeapon():GetPrimaryAmmoType() or "Pistol", false ) -- or "Pistol" to prevent erroring
if CLIENT then chat.AddText( "Ammo given from god" ) end
end )[/code]
Cleaned up your code. All the vehicle concommands are controlled by one command now: spawn_vehicle - ex. spawn_vehicle jeep. Not sure what your were trying to do with crazymode, but hopefully that works. You'll have to fill out the rest of the ent placement in the spawn_vehicle function; use the [URL="http://wiki.garrysmod.com/"]official GMod wiki[/URL].
Hopefully this helps you into seeing how Lua is notated. You need () ALWAYS after a function, whether there are arguments or not. Also, arguments are not global. If you are trying to run something outside of a function, the argument won't exist. Lastly, you always have to go in the function argument order. Putting cmd as the first argument of concommand.Add's function will just name ply, cmd instead. You have to do function( ply, cmd ) to access the cmd. Also, chat.AddText must be clientside, hence if CLIENT then.
Thanks code_gs but now I have the following error:
[ERROR] lua/console_helperr.lua:3: attempt to index global 'chat' (a nil value)
1. unknown - lua/console_helperr.lua:3
That's because you are trying to use chat library serverside
In response to the original post... Tabbing code helps with readability. Additionally a console command callback function has 3 arguments. Player, ConsoleCommand, Arguments; Some use: ( ply, cmd, args ) to identify these; you can't leave ply out and use cmd because it'll be the second argument, not the first.
Additionally, when you use : you need to use ( )s as : in Lua means a call. If you replace the : with . then you're referencing something. However, with what you're trying to do ( I'm guessing you're trying to use some of the string functions which can be used with a string such as
[code]local _str = "blah";
print( _str:find( "h", 1 ) );
[/code]
Which outputs "4 4" because h is found in _str at character 4 and ends at character 4. If we search blah it'd be 1 4, if we searched la then 2 3...
Certain data-types allow using : to call certain functions but ( )s are needed and arguments may be needed. Now, because you did concommand.Add and added a function that only runs one command you don't need to compare the command with something else, if you wanted to run a different console command you could've used RunConsoleCommand( "ch_spawnjeep" ); or ply:ConCommand( "ch_spawnjeep" ); in the code.
Hopefully this helps.
[url]http://www.lua.org/cgi-bin/demo[/url]
[QUOTE=Axarator;46888647]Thanks code_gs but now I have the following error:
[ERROR] lua/console_helperr.lua:3: attempt to index global 'chat' (a nil value)
1. unknown - lua/console_helperr.lua:3[/QUOTE]
Oops. Change it to
[code]if CLIENT then chat.AddText( "Console Helper Initialized" ) end[/code]
Sorry, you need to Log In to post a reply to this thread.