• A function to receive arguments
    26 replies, posted
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 :p
[QUOTE=InfernalCookie;34502693][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 )[/QUOTE] "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: [QUOTE=meoiswa;34510582]Just try using PLUGIN.AddVote both when declaring and when calling, that should fix the issue[/QUOTE] 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
[QUOTE=InfernalCookie;34547437]Okay so I made them both PLUGIN.AddVote and now it's saying Unknown command: "EV_VoteYes"[/QUOTE] Still have the concommand.Add in the if(CLIENT) tag? Don't.
[QUOTE=Freze;34559075]Still have the concommand.Add in the if(CLIENT) tag? Don't.[/QUOTE] 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 > 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"
You could also do, Just noticed. if string.lower(cmd) == "ev_voteyes" then That would make it case insensitive. [editline]5th February 2012[/editline] You sure the PLUGIN:Call Is actually Called?
Between lines 1 and 2, add if !self.Voting then return end
[QUOTE=Freze;34560191]You could also do, Just noticed. if string.lower(cmd) == "ev_voteyes" then That would make it case insensitive. [editline]5th February 2012[/editline] You sure the PLUGIN:Call Is actually Called?[/QUOTE] Yes because PLUGIN:Call sends the usermessage to the client side to give everyone the Derma to vote. [editline]5th February 2012[/editline] [QUOTE=my_hat_stinks;34560271]Between lines 1 and 2, add if !self.Voting then return end[/QUOTE] That's not going to change the fact that it finds votesyes as nil, at least I don't think it will.
if !votesyes then votesyes = 1 else votesyes = votesyes + 1 end Yes?
[QUOTE=InfernalCookie;34560392]That's not going to change the fact that it finds votesyes as nil, at least I don't think it will.[/QUOTE] Yes it will You don't define votesyes until the vote is active, at which point self.Voting is also true :p
[QUOTE=my_hat_stinks;34561481]Yes it will You don't define votesyes until the vote is active, at which point self.Voting is also true :p[/QUOTE] That'll work too. But so will mine, either one.
Ok I changed it and it still thinks it's a nil value... I fixed the nil value portion but it keeps saying it's a value of 0.. :S [lua] concommand.Add( "EV_VoteYes", function( ply, cmd, args ) if !votesyes or votesyes == nil then votesyes = 0 end votesyes = votesyes + 1 end ) concommand.Add( "EV_VoteNo", function( ply, cmd, args ) if !votesno or votesno == nil then votesno = 0 end votesno = votesno + 1 end ) [/lua] Full Code: [lua] /*------------------------------------------------------------------------------------------------------------------------- Start a vote -------------------------------------------------------------------------------------------------------------------------*/ local PLUGIN = {} PLUGIN.Title = "Vote Kick" PLUGIN.Description = "Start a vote to kick a player." PLUGIN.Author = "Therius" PLUGIN.ChatCommand = "votekick" PLUGIN.Usage = '<player>' PLUGIN.Privileges = { "Vote Kick" } function PLUGIN:VoteKickEnd( targetpl2 ) SendUserMessage( "EV_VoteKickEnd", nil ) local msg = "" randomtable = { [1]="", [2]="" } yes = votesyes no = votesno for i, _ in pairs( randomtable ) do if i == 1 then local percent = 0 if ( yes == 0 ) then percent = 0 else percent = math.Round( ( yes or 0 ) / self.VotingPlayers * 100 ) end msg = msg .. " Yes" .. " (" .. percent .. "%) and " yespercent = percent elseif i == 2 then local percent = 0 if ( no == 0 ) then percent = 0 else percent = math.Round( ( no or 0 ) / self.VotingPlayers * 100 ) end msg = msg .. "No" .. " (" .. percent .. "%)" nopercent = percent end end --evolve:Notify( evolve.colors.blue, "Votekick for ", evolve.colors.red, targetpl2, evolve.colors.white, msg .. "." ) evolve:Notify( evolve.colors.blue, "Votekick for ", evolve.colors.red, targetpl2, evolve.colors.white, "Yes ( " .. yes .. " ) and No ( " .. no .. " )." ) local kickplayer = evolve:FindPlayer( targetpl2 ) if yes == self.VotingPlayers * 0.67 then kickplayer[1]:Kick( "Votekicked" ) end self.Voting = nil self.votesno = nil self.votesyes = nil end concommand.Add( "EV_VoteYes", function( ply, cmd, args ) if !votesyes or votesyes == nil then votesyes = 0 end votesyes = votesyes + 1 end ) concommand.Add( "EV_VoteNo", function( ply, cmd, args ) if !votesno or votesno == nil then votesno = 0 end votesno = votesno + 1 end ) 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 > 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:Nick() ) 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 if ( CLIENT ) then function PLUGIN:ShowVoteKickMenu( players ) self.VoteWindow = vgui.Create( "DFrame" ) self.VoteWindow:SetSize( 200, 95 ) self.VoteWindow:SetPos( ScrW() / 2 - self.VoteWindow:GetWide() / 2, ScrH() / 2 - self.VoteWindow:GetTall() / 2 ) if players then self.VoteWindow:SetTitle( "Votekick " .. players ) else self.VoteWindow:SetTitle( "Votekick " ) end self.VoteWindow:SetDraggable( false ) self.VoteWindow:ShowCloseButton( true ) self.VoteWindow:SetBackgroundBlur( true ) self.VoteWindow:MakePopup() local optionlist = vgui.Create( "DPanelList", self.VoteWindow ) optionlist:SetPos( 5, 25 ) optionlist:SetSize( 190, 65 ) optionlist:SetPadding( 5 ) optionlist:SetSpacing( 5 ) local voteyes = vgui.Create( "DButton" ) voteyes:SetText( "Yes" ) voteyes:SetTall( 25 ) voteyes.DoClick = function() RunConsoleCommand( "EV_VoteYes" ) evolve:Notify( evolve.colors.white, "Someone has voted yes!" ) self.VoteWindow:Close() end local voteno = vgui.Create( "DButton" ) voteno:SetText( "No" ) voteno:SetTall( 25 ) voteno.DoClick = function() RunConsoleCommand( "EV_VoteNo" ) self.VoteWindow:Close() evolve:Notify( evolve.colors.white, "Someone has voted no!" ) end optionlist:AddItem( voteyes ) optionlist:AddItem( voteno ) end --concommand.Add( "test_votekick", PLUGIN.ShowVoteKickMenu ) usermessage.Hook( "EV_VoteKickMenu", function( um ) local players = um:ReadString() PLUGIN:ShowVoteKickMenu( players ) end ) end if ( SERVER ) then end usermessage.Hook( "EV_VoteKickEnd", function() if ( PLUGIN.VoteWindow and PLUGIN.VoteWindow.Close ) then PLUGIN.VoteWindow:Close() end end ) evolve:RegisterPlugin( PLUGIN ) [/lua]
Sorry, you need to Log In to post a reply to this thread.