Condition not complied?!

Hey,
I have some weird problems with my if condition. So here is a snip of my code:

[lua]
local exp = ply:GetNWInt (“exp”)
local level = ply:GetNWInt (“level”)
print( "Level: "…level )
print( "EXP: "…exp )

if( level == 1 and exp >= 40 ) then
	LevelUp( ply )
	print( "Level Up!" )
end

[/lua]

Printed text:
Level: 1
EXP: 500

The condition is theoretical complied, but anyway it isn’t. :confused:

I hope you can help me. :slight_smile:

So the issue is LevelUp isn’t being called? Are you sure that it’s not LevelUp is called but then returns an error?

No Error. :confused:
Also there is no “Level Up!” Text in the console.

-snip- misread.

[editline]10th March 2011[/editline]

Where is ply defined? Can we see the whole function?

“ply” is a parameter.

The hole function:
[lua]
function CheckLevel( ply )
local exp = ply:GetNWInt (“exp”)
local level = ply:GetNWInt (“level”)
print( level )
print( exp )

if( level == 1 and exp >= 40 ) then
	print( "Level Up!" )
	LevelUp( ply )
end

if( level == 2 ) && ( exp >= 80 ) then
	LevelUp( ply )
end

if( level == 3 ) && ( exp >= 180 ) then
	LevelUp( ply )
end

if( level == 4 ) && ( exp >= 300 ) then
	LevelUp( ply )
end

if( level == 5 ) && ( exp >= 440 ) then
	LevelUp( ply )
	
	unique_id = ply:GetNWString ("SteamID")
	sql.Query("UPDATE player_bought_weapons SET deagle = 1 WHERE unique_id = '"..unique_id.."'") // Unlock Deagle
	sql.Query("UPDATE player_bought_weapons SET mac = 1 WHERE unique_id = '"..unique_id.."'") // Unlock MAC
	
	ply:PrintMessage( HUD_PRINTTALK, "New Weapons unlocked!" )
end

if( level == 6 ) && ( exp >= 600 ) then
	LevelUp( ply )
end

if( level == 7 ) && ( exp >= 780 ) then
	LevelUp( ply )
end

if( level == 8 ) && ( exp >= 980 ) then
	LevelUp( ply )
end

if( level == 9 ) && ( exp >= 1200 ) then
	LevelUp( ply )
end

if( level == 10 ) && ( exp >= 1440 ) then
	LevelUp( ply )
end

end
[/lua]

Try making a print() in every of your if-end conditions. See if it runs ANY of them

I have a print() function in my “LevelUp” function. So if it runs the function I will see that. But… no message and no error.

Did you hook this to anything?

Yes, the “CheckLevel” function will be called by another function.

[lua]if( level == 1 and exp >= 40 ) then
print( “Level Up!” )
LevelUp( ply )
end[/lua]
Try change it to use &&

[lua]if( level == 1 && exp >= 40 ) then
print( “Level Up!” )
LevelUp( ply )
end[/lua]

Edit: I wasn’t sure if Garry changed or left the Lua Standard along with the C style syntax.

I tried that some time ago. Don’t work. :frowning:

I just tested the code you posted, it works fine on my machine.

Are you sure there is absolutely no error in the console?

Also, could you post what you’re doing inside LevelUp

I’m assuming you have something like this server-side
[lua]
ply:SetNWInt(“level”,“1”)
[/lua]
This will make :GetNWInt return “1” to (didn’t thought it would but a test confirmed this)
Make sure you’re actually setting it to a number and not a string.
[lua]
ply:SetNWInt(“level”,1)
[/lua]

Thanks ralle. Found the problem. :slight_smile:

You can also set the default in GetNWInt, might also be useful for this.

On a side note making a level system with if statement spam is really not a good way to do it

Using elseif would be better.

You should make a table of functions to run when something is complete and only check if they fit the requirements for one level above them.

How would that be better it would still look like a mess. You need to work out a relationship between level and exp needed.

Since the experience difference between your levels is seemingly random, a table would work:

[lua]llocal experienceForLevelUp = { 40, 80, 180, 300, 440, 600, 780, 980, 1200, 1440 }

function CheckLevel( ply )
local exp = ply:GetNWInt( “exp” )
local level = ply:GetNWInt( “level” )

if( exp > experienceForLevelUp[level] ) then LevelUp( ply ) end

end[/lua]

What overv said. Except. local instead of llocal.