Require Help! - bad argument to pairs (table expected, got nil)
Posted: Mon Apr 26, 2021 5:31 am
ERROR MESSAGE:
Error
projectile.lua:14: bad argument #1 to 'pairs' (table expected, got nil)
Traceback
[C]: in function 'pairs'
projectile.lua:14: in function 'update'
[C]: in function 'xpcall'
INTENTION:
Trying to make a script that moves a projectile across the screen when I hit 'r' on my keyboard
PROGRAM CODE:
function love.load()
pressed = false
held = false
radius = 20
projectile = {}
speed = 50
end
function love.update(dt)
for k,v in pairs(projectile) do
projectile[k].x = projectile[k].x + (speed * dt)
end
inputManager()
end
function love.draw()
for k, v in pairs(projectile) do
love.graphics.circle("fill", v.x, v.y, radius)
end
end
function inputManager()
if love.keyboard.isDown('r') and held == false then
pressed = true
held = true
table.insert(projectile, {x = 50, y = 50})
end
end
function love.keyreleased(key, scancode)
if key == 'r' then
pressed = false
held = false
end
end
Error
projectile.lua:14: bad argument #1 to 'pairs' (table expected, got nil)
Traceback
[C]: in function 'pairs'
projectile.lua:14: in function 'update'
[C]: in function 'xpcall'
INTENTION:
Trying to make a script that moves a projectile across the screen when I hit 'r' on my keyboard
PROGRAM CODE:
function love.load()
pressed = false
held = false
radius = 20
projectile = {}
speed = 50
end
function love.update(dt)
for k,v in pairs(projectile) do
projectile[k].x = projectile[k].x + (speed * dt)
end
inputManager()
end
function love.draw()
for k, v in pairs(projectile) do
love.graphics.circle("fill", v.x, v.y, radius)
end
end
function inputManager()
if love.keyboard.isDown('r') and held == false then
pressed = true
held = true
table.insert(projectile, {x = 50, y = 50})
end
end
function love.keyreleased(key, scancode)
if key == 'r' then
pressed = false
held = false
end
end