Blind distance help! [LUA]

So I’m currently in the Blind Distance course and everything was written on javascript and I’m kind of confused as to what to do. I’m using Lua for my programming language, I managed to successfully convert one mission from java to lua but this one I can’t :confused:

any help ?

– This function finds the nearest enemy and returns the distance to it.
function nearestEnemyDistance()
local enemy = hero:findNearestEnemy()
–If there is no enemy, the function returns 0.
local result = 0
if enemy then
result = hero:distanceTo(enemy)

end

while true do
nearestEnemyDistance()
–save the result in the variable enemyDistance.
local enemyDistance = nearestEnemyDistance()
if enemyDistance > 0 then
hero:say(enemyDistance)
–If the enemyDistance is greater than 0:

    --// Say the value of enemyDistance variable.
    
end 

end
end

1 Like

bumperino I really need this solved :confused:

1 Like

what is your problem

1 Like

are you on lua or javavscript

1 Like

if the level is in javascript but you have lua selected, click on “restart” in the level’s menu (top right). This should reload the code and put it in lua!

1 Like

No, doesn’t reload in LUA. However, here is my try for the translation.
I think the bug is in the definition of the function???

function nearestEnemyDistance() 
    local enemy = hero:findNearestEnemy()
    local result
    if (enemy) then
        result = hero:distanceTo(enemy)
    else
        result = 0
    end
    return result
end


while true do
    local enemyDistance = nearestEnemyDistance()   
    if enemyDistance > 0 then
        hero:say(enemyDistance)
    else
        hero:shield(nearestFriend())    
    end 
end

A short solution without defining a function,to move on…

while true do
   local enemy = hero:findNearestEnemy()
   if enemy then
        local enemyD = hero:distanceTo(enemy)
        if enemyD > 0 then
        hero:say(enemyD)
        else   
        end 
    else
    end
end
1 Like