Play Home Contribute Github Join Us! Discourse Staff Members Github Contribution Guides Team

Level: Leave It To Cleaver Help (solved)

Hey,

so I can’t seem to beat this level when most others seemed to be relatively easy. I simply get an error every time that says “function cleaveWhenClose() not defined”. The point of the level is to define a function and I feel as if I’ve put in all the code it requires as well as tried to play with it for hours to no avail.

`-- This shows how to define a function called cleaveWhenClose
– The function defines a parameter called target

function cleaveWhenClose(target)

if hero:distanceTo(target) < 5 then
     -- Put your attack code here
     -- If cleave is ready, then cleave target
    if hero:isReady("cleave") then
        hero:cleave(target)
     -- else, just attack target!
    else 
        hero:attack(target)  
    end
end

end
– This code is not part of the function.
while true do
local enemy = hero:findNearestEnemy()
if enemy then
– Note that inside cleaveWhenClose, we refer to the enemy as target.
cleaveWhenClose(enemy)
end
end
`
This is my code. Idk if it formatted correctly.

1 Like

You need to scope your functions like you do variables.

Try:

local function cleaveWhenClose(target)
1 Like

Thanks! This has been an issue for me for variables and I had thought about it for the function but just in a weird, roundabout way. This solved it for me

1 Like

What would this look like in java script?

If you need help with the level, copy paste your code and surround it top and bottom with 3 backticks ( ` )

if(hero.distanceTo(target) < 5) {
        if(hero.distanceTo(enemy) < 5) {
        var ready = hero.isReady("cleave");
        hero.cleave(enemy); }                              
        
        else {
            hero.attack(enemy);
        }
    }
}

“I didn’t add the game page instructions.”

Did you define target or enemy in your code?

No, lol I Can’t believe I missed that, Thanks.

what deos it translate to in python?

i need help with the programe

I think there is a laguage trasnlator

1 Like

I am having a hard time with the code because when I push run it does everything except after my hero cleaves he doesn’t attack and dies after 21 seconds. Plz reply soon…

What is your code?
When you post it post it properly please.

[Essentials] How To Post/Format Your Code Correctly

Thank you.

1 Like
# The function defines a parameter called `target`
def cleaveWhenClose(target):
    if hero.distanceTo(target) < 5:
        pass
        # Put your attack code here
        # If cleave is ready, then cleave target
        ready = hero.isReady("cleave")
        hero.cleave(target)
        # else, just attack `target`!
        hero.attack(target)

# This code is not part of the function.
while True:
    enemy = hero.findNearestEnemy()
    if enemy:
        # Note that inside cleaveWhenClose, we refer to the `enemy` as `target`.
        cleaveWhenClose(enemy)

also you know how the character is supposed to attack after he cleaves there is another ogre he says “but it’s dead” and it lets the ogre attack him.

def cleaveWhenClose(target):
    if hero.distanceTo(target) < 5:
        ready = hero.isReady("cleave") # you have a boolean variable 
        # use ready before cleaving
            hero.cleave(target)
        # else, just attack `target`!
        # you have been told to use else if cleave is not ready
            hero.attack(target)

image
see the help for every item you own

is that the code :open_mouth:

so i did everything but in the end of fighting ogres a ogre pops up when theres like 3 seconds left and my code at that time is running the code find nearest enemy plz help

This shows how to define a function called cleaveWhenClose

The function defines a parameter called target

def cleaveWhenClose(target):
if hero.distanceTo(target) < 5:
pass
# Put your attack code here
# If cleave is ready, then cleave target
ready = hero.isReady(“cleave”)
hero.cleave(enemy)
else:
enemy = hero.findNearestEnemy()
hero.attack(enemy)

This code is not part of the function.

while True:
enemy = hero.findNearestEnemy()
if enemy:
# Note that inside cleaveWhenClose, we refer to the enemy as target.
cleaveWhenClose(enemy))

plzzzzzzzzzzzz help pulzzzzzzzzzzzzzzzz

Hello,

To help us see the formatting of the code, please review the link below to show you how to post your code with the formatting. That being said, I see a problem with your code.

ready = hero.isReady(“cleave”) # this should be an if statement not a variable

Your hero is waiting until the cleave is ready instead of attacking the nearest enemy.

Also, you should be using the variable target and not enemy in the function since that is the parameter provided.

hero.cleave(target)
hero.attack(target)