mat_290 Posted November 1, 2012 Share Posted November 1, 2012 The way this code works is when you hit Tab it shows the scoreboard, Well whenever I hit Tab it shows that error. What do I need to do to fix this? The error is on line 64. Error "attempt to index field '?' (a nil value)" Error Code if ( array[j][1] < smallest ) --------- [ Element Data returns ] --------- local function getData( theElement, key ) local key = tostring(key) if isElement(theElement) and (key) then return exports['[ars]anticheat-system']:c_callData( theElement, tostring(key) ) else return false end end -------------- [ Scoreboard ] --------------- local screenX, screenY = guiGetScreenSize() local width, height = 300, 420 local x = (screenX/2) - (width/2) local y = (screenY/2) - (height/2) local logowidth, logoheight = 275, 275 local logox = (x/2) - (logowidth/2) local logoy = (y/2) - (logoheight/2) local isEventHandled = false local page = 1 local a = { "Z", "W", "Y", "G", "H", "L", "P", "A", "B" } function getPlayerIDTable( ) local array = { } --[[ for key, thePlayer in ipairs ( getElementsByType("player") ) do local playerID = tonumber( getData( thePlayer, "playerid") ) if ( playerID ) then array[#array + 1] = { playerID, thePlayer } end end ]]-- for i = 1, 9 do local j = math.random( 1, 9 ) if ( array[j] == nil ) then array[i] = { j, a[ math.random( 1, 9 ) ] } end end return array end local players = { } function assemblePlayersByID( ) local array = getPlayerIDTable( ) local smallest, index, tempo for i = 1, #array do smallest = array[i][1] index = i for j = i + 1, #array do if ( array[j][1] < smallest ) then smallest = array[j][1] index = j end end -- flip arrays tempo = array[i] array[i] = array[index] array[index] = tempo end -- Sort for key, value in ipairs ( array ) do players[value[1]] = value[2] end end function removePlayerFromList( ) local playerID = tonumber( getData(source, "playerid") ) players[playerID] = nil end addEventHandler("onClientPlayerQuit", getRootElement(), removePlayerFromList) function renderPlayerList( ) dxDrawRectangle(x, y, width, height, tocolor(0, 0, 0, 200)) limit = 0 for k, v in pairs (players) do limit = limit + 1 end local clientName = getPlayerName(getLocalPlayer()):gsub("_", " ") dxDrawText("Bone County Roleplay 1.0", x + 2, y + 403, screenX, screenY, tocolor(255, 255, 255, 150), 1, "default-bold") dxDrawText("Players: (".. limit .."/50)", x + 208, y + 403, screenX, screenY, tocolor(255, 255, 255, 150), 1, "default-bold") dxDrawText("ID", x + 15, y + 13, screenX, screenY, tocolor(255, 255, 255, 150), 1, "default-bold") dxDrawText("Player", x + 60, y + 13, screenX, screenY, tocolor(255, 255, 255, 150), 1, "default-bold") dxDrawText("Ping", x + 250, y + 13, screenX, screenY, tocolor(255, 255, 255, 150), 1, "default-bold") dxDrawLine(x, y + 30, x + 299, y + 30, tocolor(255, 255, 255, 150), 1) local yTxt = (screenY/2) - (height/2) + 35 for k, v in ipairs (players) do if isElement(v) then local name = tostring( getPlayerName(v):gsub("_", " ") ) local id = tostring( getData(v, "playerid") ) local ping = tostring( getPlayerPing(v) ) local r, g, b = getPlayerNametagColor(v) if ( page == 1 ) then if k <= 24 then if name == clientName then dxDrawRectangle(x, yTxt, 300, 15, tocolor(255, 255, 255, 40)) end dxDrawText(tostring(id), x + 15, yTxt, screenX, screenY, tocolor(r, g, b, 255), 1, "default-bold") dxDrawText(tostring(name), x + 60, yTxt, screenX, screenY, tocolor(r, g, b, 255), 1, "default-bold") dxDrawText(tostring(ping), x + 250, yTxt, screenX, screenY, tocolor(r, g, b, 255), 1, "default-bold") yTxt = yTxt + 15 end elseif ( page == 2 ) then if k >= 12 and k < 36 then if name == clientName then dxDrawRectangle(x, yTxt, 300, 15, tocolor(255, 255, 255, 40)) end dxDrawText(tostring(id), x + 15, yTxt, screenX, screenY, tocolor(r, g, b, 255), 1, "default-bold") dxDrawText(tostring(name), x + 60, yTxt, screenX, screenY, tocolor(r, g, b, 255), 1, "default-bold") dxDrawText(tostring(ping), x + 250, yTxt, screenX, screenY, tocolor(r, g, b, 255), 1, "default-bold") yTxt = yTxt + 15 end elseif ( page == 3 ) then if k >= 24 and k < 48 then if name == clientName then dxDrawRectangle(x, yTxt, 300, 15, tocolor(255, 255, 255, 40)) end dxDrawText(tostring(id), x + 15, yTxt, screenX, screenY, tocolor(r, g, b, 255), 1, "default-bold") dxDrawText(tostring(name), x + 60, yTxt, screenX, screenY, tocolor(r, g, b, 255), 1, "default-bold") dxDrawText(tostring(ping), x + 250, yTxt, screenX, screenY, tocolor(r, g, b, 255), 1, "default-bold") yTxt = yTxt + 15 end elseif ( page == 4 ) then if k >= 36 and k < 60 then if name == clientName then dxDrawRectangle(x, yTxt, 300, 15, tocolor(255, 255, 255, 40)) end dxDrawText(tostring(id), x + 15, yTxt, screenX, screenY, tocolor(r, g, b, 255), 1, "default-bold") dxDrawText(tostring(name), x + 60, yTxt, screenX, screenY, tocolor(r, g, b, 255), 1, "default-bold") dxDrawText(tostring(ping), x + 250, yTxt, screenX, screenY, tocolor(r, g, b, 255), 1, "default-bold") yTxt = yTxt + 15 end elseif ( page == 5 ) then if k >= 48 and k < 72 then if name == clientName then dxDrawRectangle(x, yTxt, 300, 15, tocolor(255, 255, 255, 40)) end dxDrawText(tostring(id), x + 15, yTxt, screenX, screenY, tocolor(r, g, b, 255), 1, "default-bold") dxDrawText(tostring(name), x + 60, yTxt, screenX, screenY, tocolor(r, g, b, 255), 1, "default-bold") dxDrawText(tostring(ping), x + 250, yTxt, screenX, screenY, tocolor(r, g, b, 255), 1, "default-bold") yTxt = yTxt + 15 end elseif ( page == 6 ) then if id >= 60 and id < 84 then if name == clientName then dxDrawRectangle(x, yTxt, 300, 15, tocolor(255, 255, 255, 40)) end dxDrawText(tostring(id), x + 15, yTxt, screenX, screenY, tocolor(r, g, b, 255), 1, "default-bold") dxDrawText(tostring(name), x + 60, yTxt, screenX, screenY, tocolor(r, g, b, 255), 1, "default-bold") dxDrawText(tostring(ping), x + 250, yTxt, screenX, screenY, tocolor(r, g, b, 255), 1, "default-bold") yTxt = yTxt + 15 end elseif ( page == 7 ) then if id >= 72 and id < 96 then if name == clientName then dxDrawRectangle(x, yTxt, 300, 15, tocolor(255, 255, 255, 40)) end dxDrawText(tostring(id), x + 15, yTxt, screenX, screenY, tocolor(r, g, b, 255), 1, "default-bold") dxDrawText(tostring(name), x + 60, yTxt, screenX, screenY, tocolor(r, g, b, 255), 1, "default-bold") dxDrawText(tostring(ping), x + 250, yTxt, screenX, screenY, tocolor(r, g, b, 255), 1, "default-bold") yTxt = yTxt + 15 end elseif ( page == 8 ) then if id >= 84 and id < 108 then if name == clientName then dxDrawRectangle(x, yTxt, 300, 15, tocolor(255, 255, 255, 40)) end dxDrawText(tostring(id), x + 15, yTxt, screenX, screenY, tocolor(r, g, b, 255), 1, "default-bold") dxDrawText(tostring(name), x + 60, yTxt, screenX, screenY, tocolor(r, g, b, 255), 1, "default-bold") dxDrawText(tostring(ping), x + 250, yTxt, screenX, screenY, tocolor(r, g, b, 255), 1, "default-bold") yTxt = yTxt + 15 end elseif ( page == 9 ) then if id >= 96 and id < 120 then if name == clientName then dxDrawRectangle(x, yTxt, 300, 15, tocolor(255, 255, 255, 40)) end dxDrawText(tostring(id), x + 15, yTxt, screenX, screenY, tocolor(r, g, b, 255), 1, "default-bold") dxDrawText(tostring(name), x + 60, yTxt, screenX, screenY, tocolor(r, g, b, 255), 1, "default-bold") dxDrawText(tostring(ping), x + 250, yTxt, screenX, screenY, tocolor(r, g, b, 255), 1, "default-bold") yTxt = yTxt + 15 Link to comment
Scooby Posted November 2, 2012 Share Posted November 2, 2012 at a glance, shouldnt line 44 be: if ( array == nil ) ? Link to comment
mat_290 Posted November 2, 2012 Author Share Posted November 2, 2012 Well If I did that there is no error code but then pushing Tab doesn't bring up the scoreboard. Link to comment
Scooby Posted November 2, 2012 Share Posted November 2, 2012 maybe just add some outputs to debug to see whats happening... at least u should be able to find where its getting to and then fix whats wrong. Link to comment
myonlake Posted November 2, 2012 Share Posted November 2, 2012 if (array[j] == nil) then I don't think "then" and "if" can be on separate lines. You need to have it all in one line. Use this. Link to comment
mat_290 Posted November 2, 2012 Author Share Posted November 2, 2012 Which I fixed but I still get the same error code. Link to comment
Anderl Posted November 2, 2012 Share Posted November 2, 2012 if (array[j] == nil) then I don't think "then" and "if" can be on separate lines. You need to have it all in one line. Use this. No, you don't need. You can do this: if( ( condition ) and ( condition ) or ( condition ) ) then -- if ( condition ) then -- if ( condition ) and ( condition ) then Link to comment
mat_290 Posted November 2, 2012 Author Share Posted November 2, 2012 So then I can do it like that ok. Does anyone know what is causing this error code? Link to comment
Scooby Posted November 3, 2012 Share Posted November 3, 2012 if u made the change like i said, then it would have fixed the error. The reason ur scoreboard isnt showing is coz u need to add 'return true' at the bottom of that function because in the function 'drawPlayerList()' which is called when u press 'tab', u do: local done = assemblePlayersByID( ) if ( done ) then so its waiting for the variable 'done' to be true. Link to comment
mat_290 Posted November 3, 2012 Author Share Posted November 3, 2012 Thank you that works. But then I get a stack overflow error.... Link to comment
Anderl Posted November 3, 2012 Share Posted November 3, 2012 This means some function is calling itself much times, causing a stack overflow ( stack is the part of the memory that is allocated ) - You can see here what it is: http://en.wikipedia.org/wiki/Stack_overflow You might be able to check what is wrong on your code with what I said. I can't check it now, sorry. Link to comment
Scooby Posted November 3, 2012 Share Posted November 3, 2012 i tested the script and got no errors... it worked fine, although i had to blank out the exports line. did u change anything else? Link to comment
mat_290 Posted November 3, 2012 Author Share Posted November 3, 2012 I just change line 44 to if ( array == nil ) and then i added the return true statement to the bottom of the function. I just get the stack overflow on line 43. Link to comment
Scooby Posted November 4, 2012 Share Posted November 4, 2012 'return true' should be at the bottom of function assemblePlayersByID() Link to comment
Recommended Posts
Create an account or sign in to comment
You need to be a member in order to leave a comment
Create an account
Sign up for a new account in our community. It's easy!
Register a new accountSign in
Already have an account? Sign in here.
Sign In Now