if then elseif lines NOT being called at all!

so i am trying to make a little card game, and it uses 3d2d screens to display the cards. when i was testing a function i made to automatically draw a card on the screen, it showed up perfectly fine. there are 4 .png files of an entire suit, so there is a file for spades, diamonds, etc. but when i passed the suit of a card that was randomly generated, it completely skips the if then elseif lines as shown in the code below:
[lua]
local showCard=function(card,suit,x,y,width)
local height=width1.443
if card==13 then
card=1
elseif card==0 then
mat=backofcard
surface.SetMaterial(mat)
surface.SetDrawColor(Color(255,255,255,255))
surface.DrawTexturedRect(x,y,width,height)
return
else
card=card+1
end
local mult=0.07675
(card-1)
local material=nil
if suit==1 then material=s_s
elseif suit==2 then material=s_c
elseif suit==3 then material=s_d
elseif suit==4 then material=s_h
end
surface.SetMaterial(material)
surface.SetDrawColor(Color(255,255,255,255))
surface.DrawTexturedRectUV(x,y,width,height,0.001+mult,0.011,0.078+mult,0.457)
end
[/lua]
the error i get printed into console:
[lua]
bad argument #1 to ‘SetMaterial’ (IMaterial expected, got nil)
[/lua]
i put a MsgN(suit) in the code, it didn’t print “nil”
i put a MsgN(type(suit)), it printed “number”
i tried to even implement code that changes card data (62 would be a 5 of clubs) into two separate vars:
[lua]local suit=data/10%1*10[/lua]
then i made it MsgN that, not nil at all
then i tried putting the materials into a table instead, so i could use table[suit] to get the IMaterial back. then i MsgN’d that, and it was nil
again, this only happens when i use a randomly generated card, if i just used showCard(1,2,0,0,150), it would actually draw an ace of clubs on the 3d2d screen.
i do not know at all what the difference is between a number value from a randomly generated card and a number value right off the bat (the one that would produce a working card). i really need some lua experts on this to help me fix this, because it has stumped me for hours and i still don’t know how this could possibly be happening.

SetMaterial is a path to the material, not just “mat.” For example,


surface.SetDrawColor( 255, 255, 255, 255 ) 
surface.SetMaterial( Material( "vgui/hud_pv" ) )
surface.DrawTexturedRect( ScrW() * 0.5, ScrH() * 0.5, 50, 50 ) 

oh it isn’t the back of the card that i have trouble with, it’s the last few lines
s_s, s_c, etc and the backofcard materials are all assigned outside of the function. again, the textures render properly when used with a number given with the function (showCard(1…) but not with variables (showCard(gamecard.card,gamecard.suit,…)

Then perhaps that gamecard table has some nil values in it?

that’s what i thought, but i put MsgN(suit) in the function and i never got nil. i either got 1, 2, 3, or 4 printed in console. this is by far the most perplexing and frustrating lua problem i’ve ever had. things like this are starting to become normal for me. i go here to ask for help, but because my problem is so unique, nobody can solve it.
[editline].[/editline]
i edited the code to have some debug messages:



local showCard=function(card,suit,x,y,width)
	suit=tonumber(suit)
        local material=nil
	MsgN("SUIT = "..tostring(suit))
	if suit==1 then
		material=s_s
		MsgN("1")
	elseif suit==2 then
		material=s_c
		MsgN("2")
	elseif suit==3 then
		material=s_d
		MsgN("3")
	elseif suit==4 then
		material=s_h
		MsgN("4")
	elseif suit<=0 then
		MsgN("SUIT <= 0")
	elseif suit>4 then
		MsgN("SUIT > 4")
	else
		MsgN("SOMETHING IS TERRIBLY WRONG")
	end
	local height=width*1.443
	if card==13 then
		card=1
	elseif card==0 then
		mat=backofcard
		surface.SetMaterial(mat)
		surface.SetDrawColor(Color(255,255,255,255))
		surface.DrawTexturedRect(x,y,width,height)
		return
	else
		card=card+1
	end
	local mult=0.07675*(card-1)
	surface.SetMaterial(material)
	surface.SetDrawColor(Color(255,255,255,255))
	surface.DrawTexturedRectUV(x,y,width,height,0.001+mult,0.011,0.078+mult,0.457)
end


and this gets spammed into my console:



SUIT = 1
SOMETHING IS TERRIBLY WRONG

[ERROR] bad argument #1 to 'SetMaterial' (IMaterial expected, got nil)


where the error occurred is not important

then when i put this somewhere in the 3d2d screen rendering:



showCard(1,2,-75,-600,150)


and this gets spammed in console without any errors



SUIT = 2
2


Problem solved?

no, because that is without giving it any errors. this is what gives it errors, but still prints the first set of console spam:



showCard(gamecard.card,gamecard.suit,-75,-600,150)


gamecard.suit must be nil. There’s no other explanation.

Do print(suit,type(suit)) on the first line in that function.

i already explained how gamecard.suit isn’t nil. got this spammed:



4	number
4	number
2	number
4	number
2	number


i’m going to try and put all the suits into one .png file then have the code change what part of it is drawn.
[editline]yay[/editline]
it worked