Forum

> > CS2D > Scripts > lua error:attempt to compare nil with number
Forums overviewCS2D overview Scripts overviewLog in to reply

English lua error:attempt to compare nil with number

7 replies
To the start Previous 1 Next To the start

old lua error:attempt to compare nil with number

Louie
User Off Offline

Quote
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
addhook("join","infojoin")
function infojoin(id)
   for none, usgn in ipairs(OWNER) do
       if player(id,'usgn') == usgn then
          msg2(id,"©000255000Welcome,you are the server owner")
          msg2(id,"©000255000!cmds for list of available commands!")
          Rank[id] = 5
   end
   for none, usgn in ipairs(ADMIN) do
       if player(id,'usgn') == usgn then
          msg2(id,"©000255000Welcome,you are the server Admin")
          msg2(id,"©000255000!cmds for list of available commands!")
          Rank[id] = 4
   end
   for none, usgn in ipairs(MOD) do
       if player(id,'usgn') == usgn then
          msg2(id,"©000255000Welcome,you are the server Moderator")
          msg2(id,"©000255000!cmds for list of available commands!")
          Rank[id] = 3
   end
   for none, usgn in ipairs(MEMBER) do
       if player(id,'usgn') == usgn then
          msg2(id,"©000255000Welcome,you are the server Member")
          msg2(id,"©000255000!cmds for list of available commands!")
          Rank[id] = 2
   end
   for none, usgn in ipairs(VIP) do
       if player(id,'usgn') == usgn then
          msg2(id,"©000255000Welcome,you are a VIP")
          msg2(id,"©000255000!cmds for list of available commands!")
          Rank[id] = 1
       end
       end
       end
       end
       end 
   end     
end

addhook("leave","hasleft")
function hasleft(id,reason)
  if reason == 0 or reason == 1 then
     Rank[id] = 0
  end
end
So when the player join and he has a rank in the server then his rank number is increased(depends on the rank 0 for normal user and 5 for server owner)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
addhook("say","saycmds")
function saycmds(id,txt)
-- Kick
     if txt:sub(1,5) == "!kick" and Rank[id] >= 2 then
        local kickid = txt:sub(7,8)
        local kreason = txt:sub(10,#txt)
          if Rank[id] > Rank[kickid] then
             parse("kick "..kickid.." "..kreason)
          if Rank[id] < Rank[kickid] then
          msg2(id,"©255000000Error:Can't kick because that player has higher rank")
-- Ban(USGN and IP)
 elseif txt:sub(1,4) == "!ban" and Rank >= 3 then
        local banid = txt:sub(6,7)
        local banreason = txt:sub(9,#txt)
          if Rank[id] > Rank[banid] then
             parse("banusgn "..player(banid,'usgn').." 0 "..banreason)
             parse("banip "..player(banid,'ip').." 0 "..banreason)
          if Rank[id] < Rank[banid] then
          msg2(id,"©255000000Error:Can't ban because that player has higher rank")
          end
          end
          end
          end
     end
end
And here comes the problem,so when a ranked player(like mod,admin,owner) will kick or ban a player then if the player has a higher rank then he cannot be kicked/banned(like a Mod can't ban an Admin and Admin can't ban the Owner)
but when i try to test it i receive this error: attempt to compare nil with number,so why do i receive this error? help please.

old Re: lua error:attempt to compare nil with number

The Gajos
BANNED Off Offline

Quote
First learn Lua, then you can write scripts.


Stop using txt:sub(), because it sucks.
Learn how to work string.gmatch() function and use this function:
Spoiler >

old Re: lua error:attempt to compare nil with number

Louie
User Off Offline

Quote
@user The Gajos: Thx for the suggestion but the txt:sub() thing isn't really my problem,my main problem is how can i check if a player's rank is higher than another player(Like Admin rank is higher the Mod rank and Owner rank is the highest ),because it would be kinda weird if a Mod can kick/ban the Owner from the server or an Member can kick an Admin from the server .
1
2
3
4
5
6
7
8
-- Kick
     if txt:sub(1,5) == "!kick" and Rank[id] >= 3 then
        local kickid = txt:sub(7,8)
        local kreason = txt:sub(10,#txt)
        if Rank[id] > Rank[kickid] then  -- Problem
             parse("kick "..kickid.." "..kreason)
        if Rank[id] < Rank[kickid] >= 1 then -- Problem
          msg2(id,"©255000000Error:Can't kick because that player has higher rank")
I found out that those are my problem(the lines with -- Problem),i tried to change it to
1
if Rank[id] + Rank[kickid] >= 1
but then it gives a new error - attempt to perform arithmetic on '?' field(a nil value),i really have no idea on how to do this

old Re: lua error:attempt to compare nil with number

Joni And Friends
User Off Offline

Quote
Try with this
Click me >

Also you can use file File does not exist (15866) for easy to make commands
edited 1×, last 26.02.15 02:42:47 pm

old Re: lua error:attempt to compare nil with number

Louie
User Off Offline

Quote
@user Joni And Friends: it worked well it seems that the string.gmatch is good afterall thx guys,you've helped me alot
EDIT: I tried to make a Temp-Ban command but i get that error again(attempt to compare nil with number)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
elseif txt:sub(1,5) == "!tban" and Rank[id] >= 3 then
        ttble = totable(txt)
        local tbanid = tonumber(ttble[2])
        local tbandur = tonumber(ttble[2])
        local tbanreason = txt:sub(#tostring(tbandur)+7,#txt)
          if Rank[id] > Rank[tbanid] and tbandur > 0 and tbandur < 300 then
             parse("banusgn "..player(tbanid,'usgn').." "..tbandur.." "..tbanreason)
             parse("banip "..player(tbanid,'ip').." "..tbandur.." "..tbanreason)
          elseif Rank[id] <= Rank[banid] then
   msg2(id,"©255000000Error:Can't ban because that player has higher/equal rank")
              elseif Rank[id] < 4 then
                msg2(id,"©255000000You are not authorized to use the command!")
          end
          return 1
Though the others(kick,ban) is working but this one isn't
edited 1×, last 26.02.15 04:40:33 pm
To the start Previous 1 Next To the start
Log in to reply Scripts overviewCS2D overviewForums overview