English Attempt to compare string with number...

4 replies
Goto Page
To the start Previous 1 Next To the start
Up
olie18
User
Offline Off
* i did some more checking and it's definitely the high_score_table[2] that's returning as a string. Do i need to specify somehow that it loads the number into the table as a number instead of a string?

*Apparently doing math on high_score_table[2] after loading it makes it work properly... What am i missing in my knowledge here?

Code:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
-- loading the highscores into a table on server start --

for line in io.lines("sys/lua/highscore/highscore.txt") do 
table.insert (high_score_table, line);
end

function update_scores(id)
in_game_player_score[id]=(player(id,"score")*120)+(((player(id,"score")*2)*score_multiplier[id])+snowball_hit_score_bonus[id])
parse('hudtxt2 '..id..' 3 "©000230000Score: '..in_game_player_score[id]..'" 300 18 0')
     if in_game_player_score[id] > high_score_table[2] then -- offending line
     high_score_table[1] = player(id,"name")
     high_score_table[2] = in_game_player_score[id]
     high_score_save = io.open("sys/lua/highscore/highscore.txt", "w")
     high_score_save:write(""..high_score_table[1].."\n")
     high_score_save:write(""..high_score_table[2].."\n")
     high_score_save:close()
     parse('hudtxt 8 "©230000000Highscore - '..high_score_table[1]..': '..high_score_table[2]..'" 475 18 0')
     end
end


LUA ERROR: sys/lua/autorun/score.lua:41: attempt to compare string with number
-> sys/lua/autorun/score.lua:41: in function 'update_scores'
-> sys/lua/autorun/score.lua:62: in function <sys/lua/autorun/score.lua:53>


Code:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
-- There's also this that runs on server start which might be relevant --

check_high_score_exists = io.open("sys/lua/highscore/highscore.txt","r")
if check_high_score_exists ~= nil then 
io.close(check_high_score_exists)
parse("sv_msg Highscore loaded")
else
high_score_table[1] = "no highscore set"
high_score_table[2] = 0
high_score_save = io.open("sys/lua/highscore/highscore.txt", "w")
high_score_save:write(""..high_score_table[1].."\n")
high_score_save:write(""..high_score_table[2].."\n")
high_score_save:close()
parse('hudtxt 8 "©230000000Highscore - '..high_score_table[1]..': '..high_score_table[2]..'" 475 18 0')
end


Code:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
-- here's where the update_scores function is being called from --

addhook ("spawn","reset_scores")
function reset_scores(id)
score_multiplier[id] = 1
in_game_player_score[id] = 0
snowball_hit_score_bonus[id] = 0
parse('hudtxt2 '..id..' 4 "©000215000Score multiplier: '..score_multiplier[id]..'" 300 0 0')
glow_by_score_amount[id] = image("gfx/sprites/flare.png",1,0,200+id)
imageblend(glow_by_score_amount[id],1)                                        
imagealpha(glow_by_score_amount[id],0.5)
imagecolor(glow_by_score_amount[id],0,0,0)
update_scores(id)
end
edited 7×, last 30.11.17 03:09:11 am
30.11.17 08:06:32 am
Up
Denisgrad
User
Offline Off
Code:
1
2
3
in_game_player_score1[id]= score[id] * 120 + score[id] * 2 
in_game_player_bonus[id] = score_multiplier[id] +snowball_hit_score_bonus[id]
in_game_player_score[id] = in_game_player_score1[id] * in_game_player_bonus[id] --This is the final value you want


This will return a number of the score. I think this is what you want right. You cant multiply a string thats why you get an error.
30.11.17 09:19:45 am
Up
Devil-Thanh
GAME BANNED
Offline Off
Actually, the table that you read from a file is a string format

Code:
1
2
a = "123"
a = tonumber(a)


then
Code:
1
a = 123
Up
VADemon
User
Offline Off
Reading from files always returns text (strings). Here you push text into the table:
Code:
1
2
3
for line in io.lines("sys/lua/highscore/highscore.txt") do 
table.insert (high_score_table, line);
end

This would write the number value to the table:
Code:
1
2
3
for line in io.lines("sys/lua/highscore/highscore.txt") do 
table.insert(high_score_table, tonumber(line));
end


tonumber(text) works like this:
- if it's text and represents a valid number, it'll be converted to a number
- if it's nothing or invalid format it will return nothing or nil

Code:
1
2
3
print(tonumber("_2")) -- weird shit like that doesn't work
print(tonumber("3.14159")) -- correct
print(tonumber(" 2")) -- will also work


For all arithmetical operations Lua will try and convert strings to numbers: + - / * % ^

However, the less-than <, >, ==, <=, >=, ~= operators are not only arithmetical and return a boolean (true/false) value. That's why string is not automatically converted to number there.

PS: If this has helped you, please PM user Rainoth and tell him he should play less League.
30.11.17 10:44:13 am
Up
Yates
Reviewer
Offline Off
user VADemon has written:
PS: If this has helped you, please PM user Rainoth and tell him he should play less League.

Doesn't work.
To the start Previous 1 Next To the start