i need help with the usual day level here,s the code
– Defeat munchkins, collect coins. Everything as usual.
– Use AND to check existence and type in one statement.
while true do
local enemy = hero:findNearestEnemy()
– With AND, the type is only checked if enemy exists.
if enemy and enemy.type == “munchkin” then
hero:attack(enemy)
end
– Find the nearest item.
item = hero:findNearestItem()
-- Collect item if it exists and its type is "coin".
It is a little hard for me to tell what is going on, but this doesn’t look like javascript.
Please format your code. See this link about how to format your code. Put three backticks, then on the next line copy and paste all of your code, then put three more backticks.
You want to follow the pattern from the first part of this program
Here is the first part:
local enemy = hero:findNearestEnemy()
-- With AND, the type is only checked if enemy exists.
if enemy and enemy.type == "munchkin" then
hero:attack(enemy)
end
The idea is to do the same thing in the next part, but instead of enemies, munchkins and attacking it will be items, coins and moving.
So pretty much it’s the exact same thing as the top part but with local item = hero:findNearestItem() instead of local enemy = hero:findNearestEnemy(), and coin instead of munchkin. So if the top part is
local enemy = hero:findNearestEnemy()
if enemy and enemy.type == "munchkin" then
then the bottom part should just replace a couple of things, as I mentioned above and @Mr-Borges did as well.
Your hero needs to attack only the munchkins, so the hero checks the enemy’s type before attacking. Same deal with the items. Your hero only needs to collect the coins, right? So the hero needs to check if there is an item and if that item is a coin.
The first part is already and correctly done. Look at it and write the second part of the code using the same strategy, but instead of enemies, look at the items, and try to find coins instead of munchkins.
It’s the exact same as the first part, but instead of enemy = hero.findNearestEnemy(), write item = hero.findNearestItem() because you are looking for the items and not enemies. Also replace if enemy and enemy.type == "munchkin": with if item and item.type == "coin": because now you are looking for items with type coin instead of enemies with type munchkin.
but i now understand the item part but its still wrong but this is my code 4 now
– Defeat munchkins, collect coins. Everything as usual.
– Use AND to check existence and type in one statement.
while true do
local enemy = hero:findNearestEnemy()
– With AND, the type is only checked if enemy exists.
if enemy and enemy.type == “munchkin” then
hero:attack(enemy)
end
– Find the nearest item.
local item = hero:findNearestItem()
-- Collect item if it exists and its type is "coin".