• Argument help.
    12 replies, posted
Hi everyone, so recently I've been broadening my horizons with Lua in Garry's Mod a bit and tried to make my own slay function (similar to what you see in admin mods) just to see if I really could. Now while making it I wanted to include a reason field in there so it'd say something like [QUOTE]BlueNova slayed BlueNova for hi[/QUOTE] Or something like that. Here's what I have so far. [CODE]function slay( ply, cmd, args, reason ) if ply:IsAdmin() then local target = NULL for k, v in pairs( player.GetAll() ) do if( string.find( string.lower( v:Nick() ), string.lower( args[1] ) ) != nil ) then target = v break end end if ( IsValid( target ) ) then if not reason or reason == "" then target:Kill() for k, v in pairs( player.GetAll() ) do v:PrintMessage( HUD_PRINTTALK, ply:Nick() .. " has slayed " .. target:Nick() ) end else target:Kill() for k, v in pairs( player.GetAll() ) do v:PrintMessage( HUD_PRINTTALK, ply:Nick() .. " has slayed " .. target:Nick() .. " for " .. reason ) end end else ply:ChatPrint("Invalid Player.") end else ply:ChatPrint("No Access!") end end [/CODE] The above will output something on the lines of [QUOTE]BlueNova has slayed BlueNova for bluenova hi[/QUOTE] I'm just wondering if I could get some insight as to what I'm doing wrong. Thanks. Sorry if the code's a little sloppy, it's all a work in progress :)
How are you calling the function? What is the point of the args variable? Also you might want to make sure you don't have multiple matches
I call the function with a console command. [CODE]concommand.Add( "testing_this", slay )[/CODE] The args variable (from what I could tell with playing with it a bit) helps to grab someone's name. I've tried other loops and did some research around and so far that's all that's worked.
'reason' is basically table.concat(args). Use args[2] for the reason.
Seems to have worked for the concommand. Gonna be playing with it for a bit though. Thanks
Also instead of looping through player.GetAll you could just use [URL="http://wiki.garrysmod.com/page/Global/PrintMessage"]PrintMessage[/URL].
I've tried that before and unless I did something wrong it just returns with an error.
[QUOTE=BlueNova;52095198]I've tried that before and unless I did something wrong it just returns with an error.[/QUOTE] Probably did something wrong then :v:
[QUOTE=JasonMan34;52095304]Probably did something wrong then :v:[/QUOTE] Honestly yea :unimpressed: Guess I'll go playing with it for a bit.
Hiya I'm back, After playing around it seems to have worked but there's one thing I can't seem to find a work around. [CODE]PrintMessage( HUD_PRINTTALK, ply:Nick() .. " has annihilated " .. target:Nick() .. " because " .. args[2] )[/CODE] args[2] only prints the first word inputted, I may be missing something completely obvious but I tried several things and couldn't get anything to work so is there a way to make it print everything you input afterwards? I've even tried something like [CODE]local reason = table.concat( args, " " ) PrintMesssage( HUD_PRINTTALK, ply:Nick() .. " has annihilated " .. target:Nick() .. " because " .. reason )[/CODE]
args[1] = nil reason = table.concat(args, " ")
[code] local reason = table.concat(args, ' ', 2) [/code]
That worked dogmat, thank you so much!
Sorry, you need to Log In to post a reply to this thread.