Shatow
Shatow

Reputation: 17

Comparing function to number in lua

    start = io.read()
    if start == "start" then
    xpr = 50
    xp = 75
    function level()
        if xp >= xpr and level < 50 then
            xpr = xpr * 1.5
            level = level + 1
            io.write("Congrats you somehow didn't die and lose every bit of progress!", "\n")
            io.write("By the way your level is ", level, "\n")
            io.write("Now you must allocate your skill points now", "\n")
            sp = 6
            io.write("Strength: ", "\n")
            strsp = io.read()
            stre = stre + strsp
            if strsp < sp then
                io.write("There are only",sp,"skill points", "\n")
                str = str + 6
            end
            io.write("Dexterity: ", "\n")
            dexsp = io.read()
            dex = dex + dexsp
            if dexsp < sp then
                io.write("There are only",sp,"skill points", "\n")
                dex = dex + 6
            end
            io.write("Constuion: ", "\n")
            consp = io.read()
            con = con + consp
            if consp < sp then
                io.write("There are only",sp,"skill points", "\n")
                con = con + 6
            end
            io.write("Intelligence: ", "\n")
            intsp = io.read()
            int = int + intsp
            if intsp < sp then
                io.write("There are only",sp,"skill points", "\n")
                int = int + 6
            end
            io.write("Wisdom: ", "\n")
            wissp = io.read()
            wis = wis + wissp
            if wissp < sp then
                io.write("There are only",sp,"skill points", "\n")
                wis = wis + 6
            end
            io.write("Charisma: ", "\n")
            chasp = io.read()
            cha = cha + chasp
            if chasp < sp then
                io.write("There are only",sp,"skill points", "\n")
                cha = cha + 6
            end
        end
    end
 end
 
print(level(xp))

This is my code. But it comes back as

lua: attempt to compare function with number

How can I fix this?

Upvotes: 0

Views: 237

Answers (1)

Piglet
Piglet

Reputation: 28950

Comparing a function with a number doesn't make any sense.

function level() end is syntactic sugar for level = function () end. Just in case you're wondering why level is a function value.

level < 50 compares your global function level with 50.

You call level(xp) but level doesn't have any parameters btw.

Either rename the function or the variable you want to compare with 50. Either way you need to initialize that variable with a number value or you'll get an error for comparing nil with a number.

Upvotes: 3

Related Questions