Page 1 of 1

attempted to call nil while its a table

Posted: Wed Nov 03, 2021 1:22 am
by Coonnor
Error

player.lua:15: attempt to index global 'player' (a nil value)


Traceback

player.lua:15: in function 'load'
main.lua:13: in function 'load'
[C]: in function 'xpcall'
[C]: in function 'xpcall'

Code: Select all

Player = {}
function Player:load()
    self.x = 100
    self.y = 100
    self.width = 100
    self.height = 100
    self.bullets = {}

    player.fire = function ()
        bullet = {}
        bullet.x = player.x
        bullet.y = player.y
        table.insert(player.bullets, bullet)
    end
    SpaceCraft = love.graphics.newImage("/assets/SpaceCraft.png")
end
function Player:update(dt)
    if love.keyboard.isDown('w', 'up') then
        self.y = self.y - 350 * dt
    elseif love.keyboard.isDown('s', 'down') then
        self.y = self.y + 350 * dt
    end
    if love.keyboard.isDown(' ') then
        player.fire()
    end
end
function Player:draw()
    --love.graphics.rectangle("fill", self.x, self.y, self.width, self.height)
    love.graphics.draw(SpaceCraft,self.x,self.y)

    for _, v in pairs(player.bullets) do
        love.graphics.rectangle("fill", v.x, v.y, 10, 10)
    end
end

Re: attempted to call nil while its a table

Posted: Wed Nov 03, 2021 3:43 am
by ReFreezed
'player' should be 'self'.

Re: attempted to call nil while its a table

Posted: Wed Nov 03, 2021 7:22 am
by darkfrei
Is:
for _, v in pairs(player.bullets) do

Must:
for _, v in pairs(self.bullets) do