• Need help with new ulx script
    3 replies, posted
So I am trying to make it so that I can create a vote for a new gamemode on the side of the screen, and people can vote on it. And on completion based off of which one it will run the rcon command "gamemode {insert gamemode here}". Obviously my first try didn't work but the peculiar thing is, I didn't get any script errors or anything. I know I am new to lua coding, but I am getting better and just need a little help. Here is my code: [CODE]-- Gamemode voting local votegamemode = ulx.command( CATEGORY_NAME, "ulx votegamemode", ulx.gamemode, "!gm") function ulx.votegamemode(calling_ply, ... ) local argv = { ... } if ulx.voteInProgress then ULib.tsayError( calling_ply, "There is already a vote in progress. Please wait for the current one to end.", true ) return end for i=2, #argv do if ULib.findInTable( argv, argv[ i ], 1, i-1 ) then ULib.tsayError( calling_ply, "gamemode " .. argv[ i ] .. " was listed twice. Please try again" ) return end end if #argv > 1 then ulx.doVote( "Change gamemode to..", argv, votegamemodeDone, _, _, _, argv, calling_ply ) ulx.fancyLogAdmin( calling_ply, "#A started a votegamemode with options" .. string.rep( " #s", #argv ), ... ) else ulx.doVote( "Change gamemode to " .. argv[ 1 ] .. "?", { "Yes", "No" }, votegamemodeDone, _, _, _, argv, calling_ply ) ulx.fancyLogAdmin( calling_ply, "#A started a votegamemode for #s", argv[ 1 ] ) end end local function voteGamemodeDone( t, changeTo, ply ) local shouldChange = false if t.results[ 1 ] and t.results[ 1 ] > 0 then ulx.logServAct( ply, "#A approved the votemap" ) shouldChange = true else ulx.logServAct( ply, "#A denied the votemap" ) end if shouldChange then ulx.rcon("gamemode hideandseek") end end local function voteGamemodeDone( t, argv, ply ) local results = t.results local winner local winnernum = 0 for id, numvotes in pairs( results ) do if numvotes > winnernum then winner = id winnernum = numvotes end end local ratioNeeded = GetConVarNumber( "ulx_votemap2Successratio" ) local minVotes = GetConVarNumber( "ulx_votemap2Minvotes" ) local str local changeTo if (#argv < 2 and winner ~= 1) or not winner or winnernum < minVotes or winnernum / t.voters < ratioNeeded then str = "Vote results: Vote was unsuccessful." else str = "Vote results: Option '" .. t.options[ winner ] .. "' won, gamemode pending approval. (" .. winnernum .. "/" .. t.voters .. ")" -- Figure out the map to change to. if #argv > 1 then changeTo = t.options[ winner ] else changeTo = argv[ 1 ] end ulx.doVote( "Accept result and gamemode to " .. changeTo .. "?", { "Yes", "No" }, voteMapDone2, 30000, { ply }, true, changeTo, ply ) end ULib.tsay( _, str ) -- TODO, color? ulx.logString( str ) if game.IsDedicated() then Msg( str .. "\n" ) end end [/CODE] [editline]30th April 2015[/editline] Ok, so I did some editing and got it to work somewhat. I got the vote to appear on the screen and the command to work and appear in !menu. The issue is, after the vote is over, nothing happens. Here is my new code. [CODE]-- Gamemode voting function ulx.votegm( calling_ply, title, ... ) if ulx.voteInProgress then ULib.tsayError( calling_ply, "There is already a vote in progress. Please wait for the current one to end.", true ) return end ulx.doVote( title, { ... }, voteDone ) ulx.fancyLogAdmin( calling_ply, "#A started a vote (#s)", title ) end local votegm = ulx.command( "GamemodeVote", "ulx votegm", ulx.votegm, "!gm" ) votegm:addParam{ type=ULib.cmds.StringArg, hint="title" } votegm:addParam{ type=ULib.cmds.StringArg, hint="options", ULib.cmds.takeRestOfLine, repeat_min=2, repeat_max=10 } votegm:defaultAccess( ULib.ACCESS_ADMIN ) votegm:help( "Starts a gamemode vote." ) local function votegmDone( t ) local results = t.results local winner local winnernum = 0 for id, numvotes in pairs( results ) do if numvotes > winnernum then winner = id winnernum = numvotes end end local str if (t.options[ winner ] == "prop_hunt") then ulx.rcon("gamemode prop_hunt") elseif (t.options[ winner ] == "hideandseek") then ulx.rcon("gamemode hideandseek") else str = "Vote results: No option won because no one voted!" end end[/CODE]
I think need to reload the map after changing the gamemode.
Yeah, that would be the case, but I have reloaded the map numerous times and didn't change. I feel like the votegmDone is not working in tandum with the orginal script because I am not getting any feedback at all. [editline]30th April 2015[/editline] Note* I did change this ulx.doVote( title, { ... }, voteDone ) to this ulx.doVote( title, { ... }, votegmDone ) to match the scripts together but that didn't work either... Thanks for the reply tho :D
I solved it myself, if anyone wants the code this will work! [CODE]-- Gamemode voting local function votegmDone( t ) local results = t.results local winner local winnernum = 0 for id, numvotes in pairs( results ) do if numvotes > winnernum then winner = id winnernum = numvotes end end local str if not winner then str = "Vote results: No option won because no one voted!" else str = "Vote results: Option '" .. t.options[ winner ] .. "' won. (" .. winnernum .. "/" .. t.voters .. ")" RunConsoleCommand("ulx","rcon", "gamemode " .. t.options[ winner ]) end ULib.tsay( _, str ) -- TODO, color? ulx.logString( str ) Msg( str .. "\n" ) end function ulx.votegm( calling_ply, title, ... ) if ulx.voteInProgress then ULib.tsayError( calling_ply, "There is already a vote in progress. Please wait for the current one to end.", true ) return end ulx.doVote( title, { ... }, votegmDone ) ulx.fancyLogAdmin( calling_ply, "#A started a vote (#s)", title ) end local votegm = ulx.command( "GamemodeVote", "ulx votegm", ulx.votegm, "!gm" ) votegm:addParam{ type=ULib.cmds.StringArg, hint="title" } votegm:addParam{ type=ULib.cmds.StringArg, hint="options", ULib.cmds.takeRestOfLine, repeat_min=2, repeat_max=10 } votegm:defaultAccess( ULib.ACCESS_ADMIN ) votegm:help( "Starts a gamemode vote." ) -- Make sure your title is "Choose the gamemode you want to play" -- Make your options whatever gamemodes you have on the server --[[ ex. Title: Choose the gamemode you want to play option 1: prop_hunt option 2: hideandseek option 3: terrortown **Note** the name of the option has to be the name of the folder of your gamemode. ]] [/CODE]
Sorry, you need to Log In to post a reply to this thread.