[ERROR] addons/ulx/lua/ulx/modules/sh/jbslaynr.lua:43: attempt to index a nil value Fix?
15 replies, posted
My code is [CODE]------------------------------ Slaynr ------------------------------
slaynr_plys= { {},{} }
function ulx.slaynr( calling_ply, target_plys, num_slay )
if GetConVarString("gamemode") == "jailbreak" then
local affected_plys = {}
for i=1, #target_plys do
local v = target_plys[ i ]
if ulx.getExclusive( v, calling_ply ) then
ULib.tsayError( calling_ply, ulx.getExclusive( v, calling_ply ), true )
else
local num_slay=num_slay or 1
local new = #_G.slaynr_plys+1
_G.slaynr_plys[new][1] = v
_G.slaynr_plys[new][2] = num_slay
table.insert( affected_plys, v )
end
end
for i=1, #_G.slaynr_plys do
if _G.slaynr_plys[i][2] == 1 then
ulx.fancyLogAdmin( calling_ply, "#T will be slain ne[T][/T]xt round by #A", affected_plys )
else
ulx.fancyLogAdmin( calling_ply, "#T will be slain for the next ".. tostring(_G.slaynr_plys[i][2]) .." rounds by #A", affected_plys )
end
end
else
ULib.tsayError( calling_ply, "The current gamemode is not trouble in jailbreak", true )
end
end
local slaynr = ulx.command( CATEGORY_NAME, "ulx slaynr", ulx.slaynr, "!slaynr" )
slaynr:addParam{ type=ULib.cmds.PlayersArg }
slaynr:addParam{ type=ULib.cmds.NumArg, hint="times", ULib.cmds.optional, ULib.cmds.round }
slaynr:defaultAccess( ULib.ACCESS_ADMIN )
slaynr:help( "Slays target for a number of rounds, at the start of each round" )
function slaynr()
local SlayMessage=""
for i=1, #_G.slaynr_plys do
if _G.slaynr_plys[i][1]:Alive() then
_G.slaynr_plys[i][1]:Kill()
_G.slaynr_plys[i][2]=(_G.slaynr_plys[i][2]-1)
if _G.slaynr_plys[i][2] ==0 then
_G.slaynr_plys[i][1]:ChatPrint("You are now free to disconnect without further punishment")
end
SlayMessage=(SlayMessage .. tostring(_G.slaynr_plys[i][1]:Nick()))
if i~=1 then
SlayMessage=(SlayMessage .. ", ")
end
if _G.slaynr_plys[i][2]==0 then
table.remove(_G.slaynr_plys,i)
end
end
end
ULib.tsay(_, SlayMessage .. " has/have been slayed slayed")
local SlayMessage=nil
end
hook.Add("JailBreakRoundStart", "SlayPlayersNextRound",slaynr)
function Information()
if _G.slaynr_plys ~= nil then
for i=1, #_G.slaynr_plys do
_G.slaynr_plys[i][1]:ChatPrint("You will be slain this round. If you dissconnect before your punishment has ended you may be banned.")
end
end
end
hook.Add("PlayerSpawn", "Inform",Information)
function SaveSlayData()
for k,v in pairs(player.GetAll()) do
for i=1, #_G.slaynr_plys do
if v == _G.slaynr_plys[i][1] then
v:SetPData("rounds",_G.slaynr_plys[i][2])
end
end
end
_G.slaynr_plys= nil
end
hook.Add( "ShutDown", "Save_slay_data", SaveSlayData )
function LoadSlayData()
_G.slaynr_plys= { {},{} }
for k,v in pairs(player.GetAll()) do
if v:GetPData("rounds") ~= nil then
local new = #slaynr_plys+1
_G.slaynr_plys[new][1] = v
_G.slaynr_plys[new][2] = v:GetPData("rounds")
v:RemovePData("rounds")
end
end
end
hook.Add( "Initialize", "Load_slay_data", LoadSlayData )
------------------------------ Cslaynr ------------------------------
function ulx.cslay( calling_ply, target_plys, num_slay )
if GetConVarString("gamemode") == "jailbreak" then
local affected_plys = {}
for i=1, #target_plys do
local v = target_plys[ i ]
if ulx.getExclusive( v, calling_ply ) then
ULib.tsayError( calling_ply, ulx.getExclusive( v, calling_ply ), true )
else
local num_slay=num_slay or 1
for s=1, #_G.slaynr_plys do
if v ==_G.slaynr_plys[s][1] then
if not (_G.slaynr_plys[s][2] <= num_slay) then
_G.slaynr_plys[s][2]=_G.slaynr_plys[s][2] - num_slay
local slays_removed =num_slay
else
local slays_removed =_G.slaynr_plys[s][2]
table.remove(_G.slaynr_plys,s)
end
end
end
table.insert( affected_plys, v )
end
end
for i=1, #_G.slaynr_plys do
if _G.slaynr_plys[i][1] == v and slays_removed==1 then
ulx.fancyLogAdmin( calling_ply, "#A had ".. tostring(slays_removed) .." round of slaying removed by #A", affected_plys )
elseif _G.slaynr_plys[i][1] ~= v then
ulx.fancyLogAdmin( calling_ply, "#A had all ".. tostring(slays_removed) .." rounds of slaying removed by #A", affected_plys )
else
ulx.fancyLogAdmin( calling_ply, "#A had ".. tostring(slays_removed) .." round(s) of slaying removed by #T. ".. tostring(_G.slaynr_plys[i][2]) .."round(s) left." , affected_plys )
end
end
else
ULib.tsayError( calling_ply, "The current gamemode is not trouble in jailbreak", true )
end
end
local cslay = ulx.command( CATEGORY_NAME, "ulx cslay", ulx.cslay, "!cslay" )
cslay:addParam{ type=ULib.cmds.PlayersArg }
cslay:addParam{ type=ULib.cmds.NumArg, hint="times", ULib.cmds.optional, ULib.cmds.round }
cslay:defaultAccess( ULib.ACCESS_ADMIN )
cslay:help( "Cancel the slaying of target next round" )[/CODE]
Error is [CODE][ERROR] addons/ulx/lua/ulx/modules/sh/jbslaynr.lua:43: attempt to index a nil value
1. fn - addons/ulx/lua/ulx/modules/sh/jbslaynr.lua:43
2. Call - addons/ulib/lua/ulib/shared/hook.lua:183
3. NewRound - gamemodes/jailbreak/gamemode/core/sh_state.lua:316
4. func - gamemodes/jailbreak/gamemode/core/sh_state.lua:352
5. unknown - lua/includes/extensions/net.lua:32
[/CODE]
That's not the full code, how can we even know which line is line 43?
Advice: this is developer discussion, you can't simply paste a huge code, the error and expect someone to fix it.
this is the full code how do i show what line is what
[editline]4th February 2016[/editline]
Ok ok i get the point just not so much salt would be nice i will remove thread
[QUOTE=zZDarkWolfZz;49675611]this is the full code how do i show what line is what
[editline]4th February 2016[/editline]
Ok ok i get the point just not so much salt would be nice i will remove thread[/QUOTE]
Is line 43 this one?
[lua]_G.slaynr_plys[i][1]:ChatPrint("You are now free to disconnect without further punishment")[/lua]
Also, I said you can't expect someone to do as an advice, I didn't said I wouldn't help you fix it.
oh umm thought i had to move my bad and its this one _G.slaynr_plys[i][2]=(_G.slaynr_plys[i][2]-1)
[QUOTE=zZDarkWolfZz;49675622]oh umm thought i had to move my bad and its this one _G.slaynr_plys[i][2]=(_G.slaynr_plys[i][2]-1)[/QUOTE]
Put the code back, please, I can't help you without the code
ok
[editline]4th February 2016[/editline]
fixed
Try replacing line 41 with this:
[lua]if IsValid(_G.slaynr_plys[i]) and _G.slaynr_plys[i][1]:Alive() then[/lua]
Also, this code is extremely inefficient and confusing, I'd recommend you try to find a better one if possible.
Do you know a better one?
[QUOTE=zZDarkWolfZz;49675710]Do you know a better one?[/QUOTE]
Unfortunately no, but I could give a try at making one if you're unable to find a better one.
Ok thanks and what do you mean by inefficient?
[editline]4th February 2016[/editline]
I know what it means im just saying whats wrong with it?
[QUOTE=zZDarkWolfZz;49675723]Ok thanks and what do you mean by inefficient?
[editline]4th February 2016[/editline]
I know what it means im just saying whats wrong with it?[/QUOTE]
From what I'm seeing, it adds an entry to the slaynr_plys table every time you use slaynr on someone, and that means if you use slaynr on the same person again, instead of adding another slay to the player's entry, it'd add a new entry to the table.
And the saving & retreiving of rounds to be slayed for incase the player leaves, server shuts down or other occasions does not work unless the player is in the server when it goes back up and also does not work if the server crashes.
Every time a player spawns, it'll check for all pendind slays and print them in everyone's chat, that means if 20 players with slays spawn, it'll print 20 times that they are going to get slayed.
I could list some more, but I think you get me already.
Oh i see what you mean yeah i will try to look for a new code but while im at it can u try to make one? i cant i am trying to learn lua but i cant find anything to help me other than youtube which isnt really good.
[QUOTE=zZDarkWolfZz;49675751]Oh i see what you mean yeah i will try to look for a new code but while im at it can u try to make one? i cant i am trying to learn lua but i cant find anything to help me other than youtube which isnt really good.[/QUOTE]
These are some of the resources I used:
- [url=http://www.lua.org/manual/5.1/manual.html]Lua 5.1 Manual[/url]
- [url=http://www.lua.org/pil/contents.html] Programming In Lua (PIL) book[/url]
- [url=http://wiki.garrysmod.com/]New GMod Wiki[/url]
And I'll give it a shot while you search, you can add me on steam if you'd like, the link is below my profile picture.
Thanks! Will add you.
[QUOTE=zZDarkWolfZz;49675751]Oh i see what you mean yeah i will try to look for a new code but while im at it can u try to make one? i cant i am trying to learn lua but i cant find anything to help me other than youtube which isnt really good.[/QUOTE]
Engaging in a project is probably the best way to learn how to code. It allows you to have objectives to work towards and feel a sense of fufilment when done. It also gives you specific things to lookup and learn. I suggest you make an attempt at this and break it down into little pieces looking for little stuff that will help you solve these small problems. There are all kinds of resources out there already, youtube, wiki, lua wesbites.
Sorry, you need to Log In to post a reply to this thread.