Author Topic: Need help with a VoteGrav command  (Read 266 times)

0 Members and 1 Guest are viewing this topic.

Offline Mario Mom

  • Newbie
  • *
  • Posts: 1
  • Karma: 0
    • View Profile
Need help with a VoteGrav command
« on: January 09, 2017, 12:25:46 PM »
I'm trying to create a simple votegrav command, which would allow users to choose between low and default gravity. And, to an extent, it works; the first time it is called, it performs its function exactly. on subsequent uses, however, it only shows the voting prompt - nothing happens afterward. I have linked a pastebin with the source below; the commented out code is my original attempt at the  command, and the uncommented is the vote and votedone functions copy-pasted and slightly reworked to fit my needs. The issue is consistent across both versions. (only copied part of the code)

also worth noting is that the server console shows the following error after all players have voted
Code: [Select]
Lua Error: addons/ulx/lua/ulx/log.lua:479: bad argument #2 to 'format' (number expected, got string)
« Last Edit: January 09, 2017, 12:27:45 PM by Mario Mom »

Offline Timmy

  • Respected Community Member
  • Full Member
  • *****
  • Posts: 150
  • Karma: 122
  • Code monkey
    • View Profile
Re: Need help with a VoteGrav command
« Reply #1 on: January 09, 2017, 09:09:58 PM »
The "Default" option always wins
Lua has 1-based arrays. That means the first element in a table has an index of 1.

The table with outcomes has 2 elements: "Low" with an index of 1, and "Default" with an index of 2.
Code: Lua
  1. {"Low", "Default"} -- Possible outcomes of the vote

In the beginning of the voteGravDone function, you calculate the winner. The index of the winning element is assigned to the winner variable. That means winner will be 1 if the first option won, 2 if the second option won, nil if no one voted.

So when you're checking for the winning option...
Code: Lua
  1. if winner == 0 then -- line 25,
This statement will always be false: there are no elements with an index of 0.

ulx.rcon is called with a bad argument, causing Lua errors
Code: [Select]
Lua Error: addons/ulx/lua/ulx/log.lua:479: bad argument #2 to 'format' (number expected, got string)Caused by calling ulx.rcon with nil (instead of a Player) as the first argument. That particular function is a callback for a command. It is not really meant to be called directly.
Code: Lua
  1. ulx.rcon( _, "sv_gravity 250" ) -- Bad!
You can use RunConsoleCommand instead.
Code: Lua
  1. RunConsoleCommand( "sv_gravity", 250 )
« Last Edit: January 09, 2017, 09:34:25 PM by Timmy » (PGP key) • Steam • GitHub