[SOLVED] Backwoods forest : The one wizard challenge help

So i was tinkering on this level a bit until i find a way to fix the level that doesn’t let me load and i decided to play this level. I can easily do one or two tasks but im trying to get the other bonus but i ran into a problem that i dont understand.
Here is the code, i know it can be improved a bit but i just want to know whats causing this error i will give screenshots with the code and I will place it in text:

You can see the error that its giving me, now here is the code in text:

while true do
    local target = hero:findNearestEnemy()
if target and hero:canCast("chain-lightning", target) then
    hero:cast("chain-lightning", target)
    elseif target and  target.type == "scout" or target.type == "ogre" or target.type == "catapult" and hero:canCast("lightning-bolt", target) then
        hero:cast("lightning-bolt", target)
    elseif target then


you don’t need to add the target in hero:canCast().

really ? i think i clicked tab and it pre wrote it like this.

Edit: I fixed it as you said it was wrong but it still giving the error

The if for canCast() is not in the loop

Its not in the loop ? Can you explain a bit why. Nvm i got it i was looking for something that didnt exist, now i just gotta understand how im gonna kill those bonus 44 orcs

The if target and hero:canCast(“chain-lightning”, target) is in the loop between do -> end
if hero.canCast(“chain-lightning”):
if hero.isReady(“chain-lightning”):
are equivalent
chain-lightning is a weapon of mass destruction and somehow must be ensured we target only living beings…