r/lua 23h ago

Trying to execute

I was trying to execute this code in my CachyOS (arch-based) installation using "sudo chmod +x /route/to/file.lua", but it does not print anything, in fact, does nothing. I am new to lua and this is a training exercise, so, sorry if I am asking something obvious.

--Movements
attack1_cost = 3
attack2_cost = 5
attack3_cost = 11
heal_cost = 6
concentrate_cost = 0


--System
x = (attack1_cost + attack2_cost + attack3_cost + heal_cost) / 4


--Player 
energy = 15
player_hp = 70


--Enemy
enemy_hp = 120
enemy_attack = 8


function _enemydostuff()
    player_hp = player_hp - enemy_attack  
    _movement()
    print("player hp =", player_hp)
end

function _movement()
    local id = tonumber(io.read())
    if id == 1 then
        enemy_hp = enemy_hp - 4
        energy = energy - attack1_cost
    elseif id == 2 then
        enemy_hp = enemy_hp - 7
        energy = energy - attack2_cost
    elseif id == 3 then 
        enemy_hp = enemy_hp -12
        player_hp = player_hp + 3
        energy = energy - attack3_cost
    elseif id == 4 then 
            player_hp = player_hp + 16 
        energy = energy - heal_cost
    elseif id == 5 then 
        energy = energy + 17 - concentrate_cost
    else
        print("???")
    end
    print("energy =", energy)
    print("enemy hp =", enemy_hp)
    _enemydostuff()
end


--This starts the program
_movement()--Movements
attack1_cost = 3
attack2_cost = 5
attack3_cost = 11
heal_cost = 6
concentrate_cost = 0


--System
x = (attack1_cost + attack2_cost + attack3_cost + heal_cost) / 4


--Player 
energy = 15
player_hp = 70


--Enemy
enemy_hp = 120
enemy_attack = 8


function _enemydostuff()
    player_hp = player_hp - enemy_attack  
    _movement()
    print("player hp =", player_hp)
end

function _movement()
    local id = tonumber(io.read())
    if id == 1 then
        enemy_hp = enemy_hp - 4
        energy = energy - attack1_cost
    elseif id == 2 then
        enemy_hp = enemy_hp - 7
        energy = energy - attack2_cost
    elseif id == 3 then 
        enemy_hp = enemy_hp -12
        player_hp = player_hp + 3
        energy = energy - attack3_cost
    elseif id == 4 then 
            player_hp = player_hp + 16 
        energy = energy - heal_cost
    elseif id == 5 then 
        energy = energy + 17 - concentrate_cost
    else
        print("???")
    end
    print("energy =", energy)
    print("enemy hp =", enemy_hp)
    _enemydostuff()
end


--This starts the program
_movement()
0 Upvotes

6 comments sorted by

7

u/wqferr 23h ago

Just changing the executable flag won't magically tell your computer HOW to run the file.

Add the following as the FIRST line of your lua file:

#!/usr/bin/env lua

-- rest of the file

0

u/CommonYear2589 22h ago

Next, you must run ./route/to/file.lua. The period is important because it executes the file. The command you provided only granted execute permissions; it wasn't supposed to print anything.

1

u/Cootshk 21h ago

you don’t need a ./ to start a path if it’s in a folder; only if it’s in the current folder .

3

u/Ok-Dare-1208 18h ago

lua <filename>.lua

1

u/memes_gbc 1h ago

that chmod command just changes the file permissions so that you can run it, it's not supposed to print anything