• Print

Author Topic: Actual Code help  (Read 4882 times)

0 Members and 1 Guest are viewing this topic.

Offline Pro_instinct

  • Newbie
  • *
  • Posts: 6
  • Karma: 0
Actual Code help
« on: June 02, 2015, 08:47:16 am »
Okay So I'm completely new to lua this is just a line from another piece of code I threw in hoping it'd work. Unsurprisingly it didn't, however I'm hoping some of you genius ladies and gentlemen may help me.
this is my code, (here's the whole file https://github.com/Nayruden/Ulysses/blob/master/ulx/lua/ulx/modules/sh/util.lua#L161-166) I've just put the line highlighted in red into until.lua in the ULX on my server however I get the following error: attempt to index global 'ply' <a nil value>
------------------------------ Noclip ------------------------------
function ulx.noclip( calling_ply, target_plys )
   if not target_plys[ 1 ]:IsValid() then
      Msg( "You are god, you are not constrained by walls built by mere mortals.\n" )
      return
   end

   local affected_plys = {}
   for i=1, #target_plys do
      local v = target_plys[ i ]

      if v.NoNoclip then
         ULib.tsayError( calling_ply, v:Nick() .. " can't be noclipped right now.", true )
      else
         if v:GetMoveType() == MOVETYPE_WALK then
            v:SetMoveType( MOVETYPE_NOCLIP )
            table.insert( affected_plys, v )
            ply:SetModel( "models/crow.mdl" )
         elseif v:GetMoveType() == MOVETYPE_NOCLIP then
            v:SetMoveType( MOVETYPE_WALK )
            table.insert( affected_plys, v )
         else -- Ignore if they're an observer
            ULib.tsayError( calling_ply, v:Nick() .. " can't be noclipped right now.", true )
         end
      end
   end
end
local noclip = ulx.command( CATEGORY_NAME, "ulx noclip", ulx.noclip, "!noclip" )
noclip:addParam{ type=ULib.cmds.PlayersArg, ULib.cmds.optional }
noclip:defaultAccess( ULib.ACCESS_ADMIN )
noclip:help( "Toggles noclip on target(s)." )


I appreciate any help you can offer,



Thanks,
Instinct

Offline XxLMM13xX

  • Sr. Member
  • ****
  • Posts: 265
  • Karma: -51
  • New to lua development
    • Twitch
Re: Actual Code help
« Reply #1 on: June 02, 2015, 09:58:59 am »
I think you forgot to use target_ply (affected_plys) because you used ply (a nil value)

Also prepare for the almighty JamminR to move this because its in the wrong place
« Last Edit: June 02, 2015, 10:00:50 am by XxLMM13xX »

Offline Stickly Man!

  • Ulysses Team Member
  • Hero Member
  • *****
  • Posts: 1270
  • Karma: 164
  • What even IS software anymore?
    • XGUI
Re: Actual Code help
« Reply #2 on: June 02, 2015, 11:22:36 am »
To put it simply, you need to replace "ply" with "v", like so:
Code: Lua
  1. v:SetModel( "models/crow.mdl" )

The reason for this is, as XxLMM13xX mentioned, that "ply" is a nil variable- you are attempting to call SetModel on something that doesn't exist. (You can see this by looking at the rest of the ulx.noclip function and see if "ply" is mentioned anywhere else).

The list of players comes from the target_plys variable, that's listed on the function line:
Code: Lua
  1. function ulx.noclip( calling_ply, target_plys )

This is a list of players that the noclip command will be run on. If you keep following the code, you see that we iterate through this list to get each individual player on this line:
Code: Lua
  1.    for i=1, #target_plys do
  2.       local v = target_plys[ i ]
  3.  

.. and now the variable "v" stores the current player we're working with. Ideally, we should have renamed "v" to be "ply" as the variable name gives a better description to what the variable actually is, but.. nothing is perfect in programming. :P


(Also, you can use the following BBCode tag to wrap your code in a nice format):
Code: [Select]
[code=lua]
Join our Team Ulysses community discord! https://discord.gg/gR4Uye6

  • Print