A function to receive arguments

So I need to make a function that if the console command is called then it will add a value to a variable… So what I have now is:

[lua]
function PLUGIN:AddVote( ply, cmd, args )
if args == 1 then
voteyes = voteyes + 1
elseif args == 2 then
voteno = voteno + 1
end
end
if ( CLIENT ) then
concommand.Add( “EV_VoteYes”, PLUGIN.AddVote )
end
[/lua]

If youre getting errors, and I supouse so because you haven’t said it, the parameters of a function called with a concommand are:
ply = player that used the command
cmd = name of the command used (this is because theoretically two commands could call the same function)
args = TABLE with the arguments

So if you want a command “EV_VoteYes” that adds +1 to the variable voteyes, you need:
SERVERSIDE:
[lua]
function PLUGIN:AddVote( ply, cmd, args )
if args[1] == 1 then
voteyes = voteyes + 1
elseif args[1] == 2 then
voteno = voteno + 1
end
end
concommand.Add( “EV_VoteYes”, PLUGIN:AddVote ) --Functions defined with table:function are called with table:function and not table.function
–Remember that “:” denotes a function of the table, while “.” denotes a property of that table (which could hold a function)
[/lua]

This will add a concommand in the server, avaliable for all players to use, that will trigger the function PLUGIN:AddVote on the server

I’d do it this way:

[lua]
function PLUGIN:Vote(ply, cmd, args)
if cmd == “vote_yes” then
voteyes=voteyes+1
elseif cmd== “vote_no” then
voteno=voteno+1
end
end
concommand.Add(“vote_yes”,PLUGIN:Vote)
concommand.Add(“vote_no”,PLUGIN:Vote)
[/lua]

This will add two concommands, vote_yes and vote_no, both will call the function PLUGIN:Vote() but will cause it to add to the voteyes or voteno accordingly

Oh god, you’re right. I forgot that args is a table and I need to check the first item of the table… But I’m not getting any errors. Let me try that…

Check my post again I have this bad habit of writing, posting and then proofreading/editing hundreds of times until its perfect :b

[lua]
function PLUGIN:AddVote( ply, cmd, args )
if args[1] == 1 then
voteyes = voteyes + 1
elseif args[1] == 2 then
voteno = voteno + 1
end
end
concommand.Add( “EV_VoteYes”, PLUGIN.AddVote )

– My clientside command to call the command… BTW, this is in a derma button that each player receives.
RunConsoleCommand( “EV_VoteYes”, 1 )
[/lua]

ERROR: attempt to index local ‘args’ (a nil value)

[editline]2nd February 2012[/editline]

Okay, let me try it the new way.

[editline]2nd February 2012[/editline]

function arguments expected near ‘)’
line 58:
concommand.Add( “EV_VoteYes”, PLUGIN:AddVote )

Are you sure the function is getting defined properly? try using PLUGIN.AddVote instead of PLUGIN:AddVote (Both in the statement function and in the concommand.Add)

I know the function is being called because I tested it before and it gave me an error that would only occur if the function was called.

Should it not be:
concommand.Add( “EV_VoteYes”, PLUGIN.AddVote, PLUGIN )

Or am I just being silly?

Edit: No, that’s for timers, I’m being silly, just ignore this :stuck_out_tongue:

“function arguments expected near ‘<something>’” usually means you need to put “()” at the end of a method (in this case PLUGIN:AddVote -> PLUGIN:AddVote()). Though from what I can see on the wiki concommand.Add wants the function as a variable (without ‘()’), not a method/function call… Make sure you haven’t put too many parenthesis somewhere in the script.

Just try using PLUGIN.AddVote both when declaring and when calling, that should fix the issue

D’oh. I don’t think you’re supposed to define PLUGIN:AddVote as a method for PLUGIN. Just rename the function into plain AddVote.

EDIT: Actually yes, the above post is a better solution. Disregard what I said. Using a colon means you’re trying to use it as a method on the PLUGIN object, while using a dot fetches the function as a variable.

Or do what he said:

That will work.

Okay so I made them both PLUGIN.AddVote and now it’s saying Unknown command: “EV_VoteYes”

That usually means that the concommand.Add didnt run correctly, most probably because the function it points to didnt compile correctly.
Make sure there are no other error messages prior to that

Still have the concommand.Add in the if(CLIENT) tag?

Don’t.

I tried it with the concommand in the client part and outside.

[lua]
function PLUGIN.AddVote( ply, cmd, args )
if cmd == “EV_VoteYes” then
voteyes = voteyes + 1
evolve:Notify( evolve.colors.blue, “someone has voted yes” )
elseif cmd == “EV_VoteNo” then
voteno = voteno + 1
end
end
concommand.Add( “EV_VoteYes”, PLUGIN.AddVote() )
concommand.Add( “EV_VoteNo”, PLUGIN.AddVote() )
[/lua]

Try posting all of the code you’ve got right now.

^ Beat you to it, refresh

[lua]
concommand.Add( “EV_VoteYes”, PLUGIN.AddVote )
concommand.Add( “EV_VoteNo”, PLUGIN.AddVote )
[/lua]

You don’t want braces when pointing to the function. You use () when you call it.

Ok, I just added those in because I used to get errors when not having them, but that’s when I had a colon and not a period. Let me try it without the parenthesis. Thanks

[editline]5th February 2012[/editline]

Now it does call the function but for some reason it doesn’t do the arithmetic of “voteyes = voteyes + 1” because it’s a nil value. I thought I declared it in the PLUGIN:Call function…

[lua]
function PLUGIN.AddVote( ply, cmd, args )
if cmd == “EV_VoteYes” then
votesyes = votesyes + 1
evolve:Notify( evolve.colors.blue, “someone has voted yes” )
elseif cmd == “EV_VoteNo” then
votesno = votesno + 1
end
end
concommand.Add( “EV_VoteYes”, PLUGIN.AddVote )
concommand.Add( “EV_VoteNo”, PLUGIN.AddVote )

function PLUGIN:Call( ply, args )
if ( ply:EV_HasPrivilege( “Vote Kick” ) ) then
if ( self.Voting ) then
evolve:Notify( ply, evolve.colors.red, “You can’t start a new vote kick until the current one has finished!” )
return
elseif ( #player.GetAll() < 3 ) then
evolve:Notify( ply, evolve.colors.red, “There aren’t enough players to start a vote kick!” )
return
end

	local targetpl = evolve:FindPlayer( args[1] )
	
	if ( #targetpl == 0 ) then
		evolve:Notify( ply, evolve.colors.red, "You haven't specified a player!" )
	elseif ( #targetpl &gt; 1 ) then
			evolve:Notify( ply, evolve.colors.white, "Did you mean ", evolve.colors.red, evolve:CreatePlayerList( targetpl, true ), evolve.colors.white, "?" )
			
			return
	elseif ( #targetpl == 1 ) then
		targetpl = targetpl[1]
		uid = targetpl:UniqueID()
		

		self.Voting = true
		votesyes = 0
		votesno = 0
		self.Votes = {}
		self.VotingPlayers = 0
		self.VotingPlayers = table.Count( player.GetAll() )
		
		umsg.Start( "EV_VoteKickMenu" )
			umsg.String( tostring( targetpl:Nick() ) ) 
			umsg.Short( votesyes )
			umsg.Short( votesno )
		umsg.End()
		
		timer.Create( "EV_VoteKickEnd", 10, 1, function() PLUGIN:VoteKickEnd( targetpl ) end )
		
		evolve:Notify( evolve.colors.blue, ply:Nick(), evolve.colors.white, " has started the vote kick for ", evolve.colors.red, targetpl:Nick(), evolve.colors.white, "." )
	end
else
	evolve:Notify( ply, evolve.colors.red, evolve.constants.notallowed )
end

end
[/lua]

WOOPS. I made a mistake I found myself… Hold on let me test it now that I fixed this. Okay I changed it and it’s still “voteyes is a nil value”