PDA

View Full Version : Attempt to compare two nil values error in Lua Please Help



splodger15
December 26th, 2006, 21:09
I am getting this error in my script can someone help me out.

game.lua:173:attempt to compare two nil values

It seems to be the bit in bold on the script

-- Created by splodger15

----------detect other cars collision--------------
function detectCollision(player, opponent)
if (player.x + character:width() > opponent.x) and (player.x < opponent.x + opponent.pic:width()) and (player.y + character:height() > opponent.y) and (player.y < opponent.y + opponent.pic:height()) then
screen.waitVblankStart(60)
score = score - 1
end
end


Room_width = 480
Room_height = 272
player1x = 15
player1y = 136
road=Image.load("images/road.PNG")
background=road
car1=Image.load("images/car1.PNG")
player1=car1
car2=Image.load("images/car2.PNG")
car3=Image.load("images/car3.PNG")
car4=Image.load("images/car4.PNG")
score = 0

----------walls------------

wall1 = Image.createEmpty(480,2)
wall2 = Image.createEmpty(2,272)
wall3 = Image.createEmpty(2,272)
wall4 = Image.createEmpty(480,2)

wall1 = {x = 0, y = 0, height = wall1:height(), width = wall1:width() }
wall2 = {x = 450, y = 0, height = wall2:height(), width = wall2:width() }
wall3 = {x = 0, y = 0, height = wall3:height(), width = wall3:width() }
wall4 = {x = 0, y = 299, height = wall4:height(), width = wall4:width() }



player = {}
player.x = 25
player.y = 100
playerHeight = 63
playerWidth = 33

car2 = {}
car2.x = 128
car2.y = 90
car2Height = 63
car2Width = 33
car2.pic = Image.load("images/car2.PNG")

car3 = {}
car3.x = 228
car3.y = 90
car3Height = 63
car3Width = 33
car3.pic = Image.load("images/car3.PNG")

car4 = {}
car4.x = 28
car4.y = 20
car4Height = 63
car4Width = 33
car4.pic = Image.load("images/car4.PNG")

character = car1

------------------player sprite loading--------------

function rands1()
rand2 = math.random(1,3)
end

function rands2()
rand3 = math.random(1,3)
end

function rands3()
rand4 = math.random(1,3)
end

XScrollPosition = 480

numberOfScrolls = 0

while true do
math.randomseed(os.time() )
rand=math.random(1,3)
random1 =math.random(20,115)
random2 =math.random(90,195)
random3 =math.random(192,225)

pad = Controls.read()
screen:clear()

oldx = player.x
oldy = player.y
screen:clear()

if pad:left() then
character = car1
player.x = player.x - 2
end

if pad:right() then
character = car1
XScrollPosition = XScrollPosition - 10
if XScrollPosition < 0 then
XScrollPosition = XScrollPosition + 480
numberOfScrolls = numberOfScrolls + 1
end
end

if pad:up() then
character = car1
player.y = player.y - 2
end

if pad:down() then
character = car1
player.y = player.y + 2
end

if pad:triangle() then
dofile("map.lua")
end

if pad:square() then
Image:save("screenshot.png")
end

if pad:start() then
break
end


if rand ==1 then
car2.x = car2.x -5
car3.x = car3.x-5
car4.x = car3.x-5
end

if rand ==2 then
car2.x = car2.x -5
car3.x = car3.x-5
car4.x = car3.x-5
end

if rand ==3 then
car2.x = car2.x -5
car3.x = car3.x-5
car4.x = car3.x-5
end

if car2.x <=0 then
car2.x =510
car2.y = random1
rands1()
end

if car3.x <=0 then
car3.x = 520
car3.y = random2
rands2()
end

if car4.x <=0 then
car4.x = 500
car4.y = random3
rands3()
end

if car1X100 > car2X90 then
score = score +1
end

screen:clear()

screen:blit(XScrollPosition, 0, road)
screen:blit(2,2,score)


if XScrollPosition > 0 then
screen:blit(XScrollPosition - 480, 0, road)
else
screen:blit(XScrollPosition + 480, 0, road)
end

if numberOfScrolls > 30 then
dofile("eol.lua")
end

if player.x > 448 then
Player.x = 448
end
if player.x < 0 then
player.x = 0
end

if player.y > 240 then
player.y = 240
end
if player.y < 0 then
player.y = 0
end


detectCollision(player, car2)
detectCollision(player, car3)
detectCollision(player, car4)

-----------blit background and player------------

screen:blit(player.x, player.y, character)
screen:blit(car2.x, car2.y, car2.pic)
screen:blit(car3.x, car3.y, car3.pic)
screen:blit(car4.x, car4.y, car4.pic)
screen.flip()
screen.waitVblankStart()
end

jak66
December 27th, 2006, 00:35
the 2 variables (playerCarX100 and enemyCarX90) are not set variables, although im 99% sure that you accidentally got the names wrong. Cant help you though, since ive only just start learning lua...again, lol

splodger15
December 30th, 2006, 16:43
Is there someone that can help

PSPKOR
December 31st, 2006, 00:11
What are these command supose to do? "car1X100", "car2X90"

splodger15
December 31st, 2006, 10:35
Thats why i think it is wrong

I am no good at scoring in Lua

PSPKOR
December 31st, 2006, 16:19
Well what is it that you want them two commands to do?

splodger15
December 31st, 2006, 16:32
I was going to have it so that if the player's x axis was bigger than the enemy's x axis you have a point added

Gizmo356
December 31st, 2006, 16:38
you cant do that cause you didn't make those into variables. you can try something like this

player.x=100
enemy.x=100
points=50
while true do
screen:clear()
pad=Controls.read()
if player.x>enemy.x then
points=points+2
end

its just that you never made a variable for you enemy x axis well thats what I think cause I dont have anyway of testing it

splodger15
December 31st, 2006, 17:08
Gizmo i put in that script you showed me but i am getting this error (See Pic) I have made the line bold that has the error

Here is the code

-- Created by splodger15

----------detect other cars collision--------------
function detectCollision(player, opponent)
if (player.x + character:width() > opponent.x) and (player.x < opponent.x + opponent.pic:width()) and (player.y + character:height() > opponent.y) and (player.y < opponent.y + opponent.pic:height()) then
screen.waitVblankStart(60)
score = score - 1
end
end


Room_width = 480
Room_height = 272
player1x = 15
player1y = 136
road=Image.load("images/road.PNG")
background=road
car1=Image.load("images/car1.PNG")
player1=car1
car2=Image.load("images/car2.PNG")
car3=Image.load("images/car3.PNG")
car4=Image.load("images/car4.PNG")
score = 0
player1.x=100
car2.x=100

screen:blit(2,2,score)


---------walls------------

wall1 = Image.createEmpty(480,2)
wall2 = Image.createEmpty(2,272)
wall3 = Image.createEmpty(2,272)
wall4 = Image.createEmpty(480,2)

wall1 = {x = 0, y = 0, height = wall1:height(), width = wall1:width() }
wall2 = {x = 450, y = 0, height = wall2:height(), width = wall2:width() }
wall3 = {x = 0, y = 0, height = wall3:height(), width = wall3:width() }
wall4 = {x = 0, y = 299, height = wall4:height(), width = wall4:width() }



player = {}
player.x = 25
player.y = 100
playerHeight = 63
playerWidth = 33

car2 = {}
car2.x = 128
car2.y = 90
car2Height = 63
car2Width = 33
car2.pic = Image.load("images/car2.PNG")

car3 = {}
car3.x = 228
car3.y = 90
car3Height = 63
car3Width = 33
car3.pic = Image.load("images/car3.PNG")

car4 = {}
car4.x = 28
car4.y = 20
car4Height = 63
car4Width = 33
car4.pic = Image.load("images/car4.PNG")

character = car1

------------------player sprite loading--------------

function rands1()
rand2 = math.random(1,3)
end

function rands2()
rand3 = math.random(1,3)
end

function rands3()
rand4 = math.random(1,3)
end

XScrollPosition = 480

numberOfScrolls = 0

while true do
math.randomseed(os.time() )
rand=math.random(1,3)
random1 =math.random(20,115)
random2 =math.random(90,195)
random3 =math.random(192,225)

pad = Controls.read()
screen:clear()

oldx = player.x
oldy = player.y
screen:clear()

if pad:left() then
character = car1
player.x = player.x - 2
end

if pad:right() then
character = car1
XScrollPosition = XScrollPosition - 10
if XScrollPosition < 0 then
XScrollPosition = XScrollPosition + 480
numberOfScrolls = numberOfScrolls + 1
end
end

if pad:up() then
character = car1
player.y = player.y - 2
end

if pad:down() then
character = car1
player.y = player.y + 2
end

if pad:triangle() then
dofile("map.lua")
end

if pad:square() then
Image:save("screenshot.png")
end

if pad:start() then
break
end

if player1.x>car2.x then
score=score+2
end


if rand ==1 then
car2.x = car2.x -5
car3.x = car3.x-5
car4.x = car3.x-5
end

if rand ==2 then
car2.x = car2.x -5
car3.x = car3.x-5
car4.x = car3.x-5
end

if rand ==3 then
car2.x = car2.x -5
car3.x = car3.x-5
car4.x = car3.x-5
end

if car2.x <=0 then
car2.x =510
car2.y = random1
rands1()
end

if car3.x <=0 then
car3.x = 520
car3.y = random2
rands2()
end

if car4.x <=0 then
car4.x = 500
car4.y = random3
rands3()
end



screen:clear()

screen:blit(XScrollPosition, 0, road)



if XScrollPosition > 0 then
screen:blit(XScrollPosition - 480, 0, road)
else
screen:blit(XScrollPosition + 480, 0, road)
end

if numberOfScrolls > 30 then
dofile("eol.lua")
end

if player.x > 448 then
Player.x = 448
end
if player.x < 0 then
player.x = 0
end

if player.y > 240 then
player.y = 240
end
if player.y < 0 then
player.y = 0
end


detectCollision(player, car2)
detectCollision(player, car3)
detectCollision(player, car4)

-----------blit background and player------------

screen:blit(player.x, player.y, character)
screen:blit(car2.x, car2.y, car2.pic)
screen:blit(car3.x, car3.y, car3.pic)
screen:blit(car4.x, car4.y, car4.pic)
screen.flip()
screen.waitVblankStart()
end

Gizmo356
December 31st, 2006, 17:23
hey lets talk on msn it will be alot easyier just add me.

splodger15
December 31st, 2006, 17:27
I added you

Gizmo356
December 31st, 2006, 17:30
k i just singed in

yaustar
December 31st, 2006, 22:59
the 2 variables (playerCarX100 and enemyCarX90) are not set variables, although im 99% sure that you accidentally got the names wrong. Cant help you though, since ive only just start learning lua...again, lol
jak66 nailed it in one. Neither playerCarX100 or enemyCarX90 have been given values so by default they are initialised to nil and that is what LuaPlayer is complaining about.

splodger15
December 31st, 2006, 23:09
How can i fix it Yaustar

Gizmo356
December 31st, 2006, 23:33
give playerCarX100 and enemyCarX90 a value

yaustar
December 31st, 2006, 23:35
Rewrite the if statement so that it is legal syntax. Beyond that, until you tell us what it is supposed to mean, we can't do anything:


if car1X100 > car2X90 then
score = score +1
end

splodger15
December 31st, 2006, 23:48
Well i wanted it to if the player1 x axis was bigger than the car2 x axis i wanted the score to go up by 1

yaustar
December 31st, 2006, 23:56
if player.x > car2.x then
score = score + 1
end
That is the code, word for word for what you said above.

splodger15
January 1st, 2007, 10:38
Thanks for that Yaustar i added that to my script.But i still keep on getting this error (See pic)

Here is my latest script
-- Created by splodger15

----------detect other cars collision--------------
function detectCollision(player, opponent)
if (player.x + character:width() > opponent.x) and (player.x < opponent.x + opponent.pic:width()) and (player.y + character:height() > opponent.y) and (player.y < opponent.y + opponent.pic:height()) then
screen.waitVblankStart(60)
score = score - 1
end
end


Room_width = 480
Room_height = 272
player1x = 15
player1y = 136
road=Image.load("images/road.PNG")
background=road
car1=Image.load("images/car1.PNG")
player1=car1
car2=Image.load("images/car2.PNG")
car3=Image.load("images/car3.PNG")
car4=Image.load("images/car4.PNG")
player1.x=100
car2.x=100
score = 0

screen:blit(2,2,score)


---------walls------------

wall1 = Image.createEmpty(480,2)
wall2 = Image.createEmpty(2,272)
wall3 = Image.createEmpty(2,272)
wall4 = Image.createEmpty(480,2)

wall1 = {x = 0, y = 0, height = wall1:height(), width = wall1:width() }
wall2 = {x = 450, y = 0, height = wall2:height(), width = wall2:width() }
wall3 = {x = 0, y = 0, height = wall3:height(), width = wall3:width() }
wall4 = {x = 0, y = 299, height = wall4:height(), width = wall4:width() }



player = {}
player.x = 25
player.y = 100
playerHeight = 63
playerWidth = 33

car2 = {}
car2.x = 128
car2.y = 90
car2Height = 63
car2Width = 33
car2.pic = Image.load("images/car2.PNG")

car3 = {}
car3.x = 228
car3.y = 90
car3Height = 63
car3Width = 33
car3.pic = Image.load("images/car3.PNG")

car4 = {}
car4.x = 28
car4.y = 20
car4Height = 63
car4Width = 33
car4.pic = Image.load("images/car4.PNG")

character = car1

------------------player sprite loading--------------

function rands1()
rand2 = math.random(1,3)
end

function rands2()
rand3 = math.random(1,3)
end

function rands3()
rand4 = math.random(1,3)
end

XScrollPosition = 480

numberOfScrolls = 0

while true do
math.randomseed(os.time() )
rand=math.random(1,3)
random1 =math.random(20,115)
random2 =math.random(90,195)
random3 =math.random(192,225)

pad = Controls.read()
screen:clear()

oldx = player.x
oldy = player.y
screen:clear()

if pad:left() then
character = car1
player.x = player.x - 2
end

if pad:right() then
character = car1
XScrollPosition = XScrollPosition - 10
if XScrollPosition < 0 then
XScrollPosition = XScrollPosition + 480
numberOfScrolls = numberOfScrolls + 1
end
end

if pad:up() then
character = car1
player.y = player.y - 2
end

if pad:down() then
character = car1
player.y = player.y + 2
end

if pad:triangle() then
dofile("map.lua")
end

if pad:square() then
Image:save("screenshot.png")
end

if pad:start() then
break
end

if player.x > car2.x then
score = score + 1
end


if rand ==1 then
car2.x = car2.x -5
car3.x = car3.x-5
car4.x = car3.x-5
end

if rand ==2 then
car2.x = car2.x -5
car3.x = car3.x-5
car4.x = car3.x-5
end

if rand ==3 then
car2.x = car2.x -5
car3.x = car3.x-5
car4.x = car3.x-5
end

if car2.x <=0 then
car2.x =510
car2.y = random1
rands1()
end

if car3.x <=0 then
car3.x = 520
car3.y = random2
rands2()
end

if car4.x <=0 then
car4.x = 500
car4.y = random3
rands3()
end



screen:clear()

screen:blit(XScrollPosition, 0, road)



if XScrollPosition > 0 then
screen:blit(XScrollPosition - 480, 0, road)
else
screen:blit(XScrollPosition + 480, 0, road)
end

if numberOfScrolls > 30 then
dofile("eol.lua")
end

if player.x > 448 then
Player.x = 448
end
if player.x < 0 then
player.x = 0
end

if player.y > 240 then
player.y = 240
end
if player.y < 0 then
player.y = 0
end


detectCollision(player, car2)
detectCollision(player, car3)
detectCollision(player, car4)

-----------blit background and player------------

screen:blit(player.x, player.y, character)
screen:blit(car2.x, car2.y, car2.pic)
screen:blit(car3.x, car3.y, car3.pic)
screen:blit(car4.x, car4.y, car4.pic)
screen.flip()
screen.waitVblankStart()
end

yaustar
January 1st, 2007, 15:42
This is a relatively simple and you should take a much better effort in solving it yourself.


Room_width = 480
Room_height = 272
player1x = 15
player1y = 136
road=Image.load("images/road.PNG")
background=road
car1=Image.load("images/car1.PNG")
player1=car1
car2=Image.load("images/car2.PNG")
car3=Image.load("images/car3.PNG")
car4=Image.load("images/car4.PNG")
You make the variable player1 point to the same image as car1.


player1.x=100
car2.x=100
score = 0
You then treat it as though it is a table which it isn't and that is what it is complaining about.

This is simple to fix that you should be able to do by yourself.

The fact that later in the script, you have a set of data that represents the player attributes again shows that you haven't thought about the design of the code very much as you don't use player1x, player1y or even player1.x anywhere else in the code.

player = {}
player.x = 25
player.y = 100
playerHeight = 63
playerWidth = 33

Think about what you are coding at every stage and why every line of code exists.