Jump to content

Attempt to compare number with string


Noki

Recommended Posts

ERROR: test\c_world.lua:3: attempt to compare number with string

function setClientTime(cmd, newTime) 
    if not newTime then outputChatBox("Enter a number between 0 and 23") return end 
    if (newTime > 23) or (newTime < 0) then 
        outputChatBox("You must enter a number between 0 and 23.") 
        return  
    end 
    setTime(tonumber(newTime), 0) 
end 
addCommandHandler("settime", setClientTime) 

There's my code. I don't know why it's showing an error. I've experimented, but nothing will work. Client side.

I want to create a settime command, where you can adjsut the time in the argument after. I cannot seem to even do that with this code. I know to remove line 3, 4, 5 and 6 for it to work, but I want to tell the player that he must put a valid number in.

I don't want it just fixed, but if someone could tell me what I was doing wrong, I'd appreciate it.

Thanks. :)

Link to comment

Hi,

The structure of the handler function of addCommandHandler is:

string commandName, [string arg1, string arg2, ...] 

Which means the extra arguments (string arg1, string arg2, ...) are always strings, what you should do here is to convert the argument to number, and this works by using tonumber;

Here you go:

function setClientTime(cmd, newTime) 
    newTime = tonumber ( newTime ) 
    if not newTime then outputChatBox("Enter a number between 0 and 23") return end 
    if (newTime > 23) or (newTime < 0) then 
        outputChatBox("You must enter a number between 0 and 23.") 
        return 
    end 
    setTime(tonumber(newTime), 0) -- What is this? 
end 
addCommandHandler("settime", setClientTime) 

  • Thanks 1
Link to comment

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...