Being the terrible coder I am, I attempted to make a "ulx slapnr" command. It's not working, I'm using PData to store the the amount of damage to be slapped next round (bender180 helped me with that). This is also my first time trying to code a ULX command, so bear with me for any mistakes I do/have done.
I'm receiving this error every time I'm trying to use the command.
L 10/13/2013 - 12:01:56: Lua Error:
[ERROR] addons/decicus_ttt/lua/ulx/modules/sh/decicus_ttt_main.lua:9: attempt to call method 'GetPData' (a nil value)
1. call - addons/decicus_ttt/lua/ulx/modules/sh/decicus_ttt_main.lua:9
2. __fn - addons/ulib/lua/ulib/shared/commands.lua:943
3. unknown - addons/ulib/lua/ulib/shared/commands.lua:1296
4. Run - lua/includes/modules/concommand.lua:69
5. unknown - addons/ulib/lua/ulib/shared/commands.lua:1310
6. unknown - lua/includes/modules/concommand.lua:69
This is my code (oh lord please don't yell at me for doing mistakes). Line 1 in this code table is actually line 6. So line 9, would be line 4:
function ulx.slapnr ( calling_ply, target_ply, dmg_num )
if not GetConVarString("gamemode") == "terrortown" then ULib.tsayError( calling_ply, gamemode_error, true ) --Taken from Bender and Skillz' TTT module. Checks if the gamemode is actually TTT.
else
local slap_dmg = tonumber(target_ply:GetPData("slapnr_dmg")) or 0 --Shortcut to the PData. PData stores the damage needed to be done next round. WHY ISNT THIS WORKING :(
if dmg_num < 0 then
ULib.tsayError(calling_ply, "Damage can't be negative.", true) --Damage can't be less than 0.
elseif dmg_num > 99 then
ULib.tsayError(calling_ply, "You can't slap with more than 99!", true) --Damage can't be more than 99, as that will kill the person. This isn't a "slay next round".
elseif dmg_num == 0 then
target_ply:RemovePData("slapnr_dmg") --If the damage is 0, it will remove the PData. 0 = No slap.
else
if dmg_num ~= slap_dmg then
target_ply:SetPData("slapnr_dmg", dmg_num) --This sets a new slap damage, overrides a previous one if existant.
slapnr_msg = ("#A is slapping #T for ".. tostring(slap_dmg) .." damage next round.")
elseif dmg_num == slap_dmg then
ULib.tsayError(calling_ply, "This amount of damage is already set to the target!", true) --Example: If the target already has a slapnr on them, if someone tries to do it again with the same amount of damage, they'll get notified.
else
target_ply:SetPData("slapnr_dmg", dmg_num) --This sets the slap damage.
slapnr_msg = ("#A is slapping #T for ".. tostring(slap_dmg) .." damage next round.")
end
end
ulx.fancyLogAdmin( calling_ply, slapnr_msg, target_ply )
end
end
--ULX Command things here.
local slapnr = ulx.command( CATEGORY_NAME, "ulx slapnr", ulx.slapnr, "!slapnr" )
slapnr:addParam{ type=ULib.cmds.PlayersArg }
slapnr:addParam{ type=ULib.cmds.NumArg, min=1, default=50, hint="damage", ULib.cmds.round }
slapnr:defaultAccess( ULib.ACCESS_ADMIN )
slapnr:help( "Slaps a target next round. If you don't put any number in, it will default to 50." )
What I have tried to do is to change this:
local slap_dmg = tonumber(target_ply:GetPData("slapnr_dmg")) or 0
to this:
local slap_dmg = tonumber(target_ply:GetPData("slapnr_dmg", 0))
It printed the same error, so I really have no clue what is needed to fix this.
Feel free to correct anything else I've done wrong, any help is appreciated.