Jump to content

Tables.


pa3ck

Recommended Posts

I got some problems with tables, I'm not that good at them. Here is my code:

addEventHandler('onClientRender', getRootElement(), function() 
    if getKeyState('l') == true then 
        playerC = 0 
        playerL = 27 
        columnsP = 28 
        playersT = {} 
        pt = 0 
        local swidth, sheight = guiGetScreenSize() 
        local rx, ry = 600, 400 
        local rectangle = dxDrawRectangle(swidth / 2 - rx / 2, sheight / 2 - ry / 2, rx, ry, tocolor(0, 0, 0,150)) 
        local titleLine = dxDrawLine(swidth / 2 - rx / 2, sheight / 2 - ry / 2 + 28, (swidth / 2) + (rx / 2), sheight / 2 - ry / 2 + 28, tocolor(255, 0, 0, 255)) 
        local columnNameTitle = dxDrawText("Players",swidth / 2 - rx / 2 + 10, sheight / 2 - ry / 2 + 3, swidth, sheight, tocolor(255, 0, 0, 255),1.7) 
        local columnPingTitle = dxDrawText("Ping",swidth / 2 - rx / 2 + 228, sheight / 2 - ry / 2 + 3, swidth, sheight, tocolor(255, 0, 0, 255),1.7) 
        local columnName = dxDrawLine(swidth / 2 - rx / 2 + 220, sheight / 2 - ry / 2, swidth / 2 - rx / 2 + 220, sheight / 2 - ry / 2 + 28, tocolor(255, 0, 0, 255)) 
        local columnPing = dxDrawLine(swidth / 2 - rx / 2 + 274, sheight / 2 - ry / 2, swidth / 2 - rx / 2 + 274, sheight / 2 - ry / 2 + 28, tocolor(255, 0, 0, 255)) 
        for _,name in ipairs(getElementsByType("player")) do 
            pt = pt + 1 
            playersT[pt] = name 
            outputChatBox(#playersT) 
            playerC = playerC + 28 
            playerL = playerL + 26 
            columnsP = columnsP + 26 
            txt = dxDrawText(getPlayerName(name),swidth / 2 - rx / 2 + 10, sheight / 2 - ry / 2 + playerC, swidth, sheight, tocolor(255, 0, 0, 255), 1.4) 
            line = dxDrawLine(swidth / 2 - rx / 2, sheight / 2 - ry / 2 + playerL, (swidth / 2) + (rx / 2), sheight / 2 - ry / 2 + playerL, tocolor(255, 0, 0, 255)) 
            name = dxDrawLine(swidth / 2 - rx / 2 + 220, sheight / 2 - ry / 2, swidth / 2 - rx / 2 + 220, sheight / 2 - ry / 2 + columnsP, tocolor(255, 0, 0, 255)) 
            ping = dxDrawLine(swidth / 2 - rx / 2 + 274, sheight / 2 - ry / 2, swidth / 2 - rx / 2 + 274, sheight / 2 - ry / 2 + columnsP, tocolor(255, 0, 0, 255)) 
        end 
    end 
end) 

My problem is, that the 'outputChatBox(#playersT)' line 19 returns 1 and 2 (there are 2 players). Why is that?

Link to comment

Because of the "for" loop.

The first time it goes thru the loop, it adds 1 name to the playerT table and total number of variables in that table is 1.

The 2nd time it goes thru the loop it adds one more name to the table and total number of variables in that table is 2.

Another thing, dxDrawText and dxDrawLine return true/false, so there's no really a reason to put their returns into globab variables.

Link to comment

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 account

Sign in

Already have an account? Sign in here.

Sign In Now
  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...