Why does my table values return nil when i clearly initialized them?

Posted by user3717078 on Stack Overflow See other posts from Stack Overflow or by user3717078
Published on 2014-06-09T03:20:55Z Indexed on 2014/06/09 3:24 UTC
Read the original article Hit count: 145

Filed under:
|
|
players = {} 
function newPlayer(name)
    players[name]={x = 200, y = 100} --assign each player their x and y coordinates, which is x: 200 and y: 100
end
function checkPosition(name?) -- Do i need a parameter?
    if players[name].x == 200 and players[name].y == 100 then --says players[name].x is a nil value
        print("good")
    else
        print("bad")
    end
end

Error: attempt to index ? (a nil value)

Current Situation: The code above says players[name].x is a nil value, I would like to know why since i thought i assigned it in the function newPlayer.

© Stack Overflow or respective owner

Related posts about lua

Related posts about nil