• There is NO official Otland's Discord server and NO official Otland's server list. The Otland's Staff does not manage any Discord server or server list. Moderators or administrator of any Discord server or server lists have NO connection to the Otland's Staff. Do not get scammed!

Lua Checking my target's name from an array

Siegh

Thronar Developer
Joined
Mar 12, 2011
Messages
1,186
Solutions
1
Reaction score
510
Location
Brazil
Hello, as the thread's name implies, I'm trying to, in a spell, check my target's name if it matches a few entries written in an array inside the global.lua. Here's what I mean:

global.lua
Code:
undeadCheck = {("Skeleton"), ("Skeleton Guardian"), ("Blood Skeleton"), ("Apparition"), ("Ghoul"), ("Skeleton Bear"), ("Slime"), ("Aberration")}

spell.lua
Code:
if target == isInArray(undeadCheck, target) then
    doPlayerSendTextMessage(cid, MESSAGE_STATUS_CONSOLE_BLUE, "TEST WORKED")
end

Nothing happens regarding this part of the script, but I also get no errors on the console.

I know it's not done properly, I tried to adapt another similar code but to no effect. Please help me solve this simple problem :D
 
Solution
maybe the creature names and check is case sensitive?
I honestly don't know where this case-sensitive thing came from, every script I see like this uses lower() on the creature name to check for it, which is useless because technically that'd create even more name conflicts (assuming using lower() is intended to reduce name conflicts).

I've already removed the () :D
Try this:
Lua:
function onCastSpell(cid, var)
    local target = getCreatureTarget(cid)
    if target > 0 then
        if isInArray(undeadCheck, getCreatureName(target)) then
            doSendMagicEffect(getCreaturePosition(cid), 10)
        end
    end
    return true
end

Would've been done in the first post if you would've provided the entire...
isInArray returns a boolean, you don't need to use the equality operator to check for anything, just do:
Lua:
if isInArray(undeadCheck, getCreatureName(target)) then
If isInArray returns true, it will continue, otherwise it returns false and will not continue.

You also don't need to add parentheses around each string in your array.
 
Last edited:
isInArray returns a boolean, you don't need to use the equality operator to check for anything, just do:
Lua:
if isInArray(undeadCheck, target) then
If isInArray returns true, it will continue, otherwise it returns false and will not continue.

You also don't need to add parentheses around each string in your array.

Thanks! I've fixed those.

Still, it doesn't work.
 
Edited my post, I'm assuming you're using an old TFS version cause you didn't specify (which you should, read the rules).
 
Edited my post, I'm assuming you're using an old TFS version cause you didn't specify (which you should, read the rules).

I'm using a 0.2.15 (Mystic Spirit), sorry for not clarifying it first.

It still didn't work... does it has something to do with using strings instead of numerical values in the array?
 
There's no errors left in that code snippet. Did you register the event to the player in login.lua (also assuming you're using an onKill creaturescript)?
 
I'm using that on a spell function (just clarifying that I've declared target = getCreatureTarget(cid) since it's not by default). And I'm casting the spell on a creature named exactly "Skeleton", contained in the undeadCheck array.

The script does the magiceffect if I check for FALSE, so it's working except for the name checking part.

Lua:
function onCastSpell(cid, var)
if isInArray(undeadCheck, getCreatureName(target)) then
    doSendMagicEffect(getCreaturePosition(cid), 10)
end

return true
end
 
I'm using that on a spell function (just clarifying that I've declared target = getCreatureTarget(cid) since it's not by default). And I'm casting the spell on a creature named exactly "Skeleton", contained in the undeadCheck array.

The script does the magiceffect if I check for FALSE, so it's working except for the name checking part.

Lua:
function onCastSpell(cid, var)
if isInArray(undeadCheck, getCreatureName(target)) then
    doSendMagicEffect(getCreaturePosition(cid), 10)
end

return true
end
remove all (), put like this:
Code:
undeadCheck = {"Skeleton", "Skeleton Guardian", "Blood Skeleton", "Apparition", "Ghoul", "Skeleton Bear", "Slime", "Aberration"}
 
maybe the creature names and check is case sensitive?
I honestly don't know where this case-sensitive thing came from, every script I see like this uses lower() on the creature name to check for it, which is useless because technically that'd create even more name conflicts (assuming using lower() is intended to reduce name conflicts).

I've already removed the () :D
Try this:
Lua:
function onCastSpell(cid, var)
    local target = getCreatureTarget(cid)
    if target > 0 then
        if isInArray(undeadCheck, getCreatureName(target)) then
            doSendMagicEffect(getCreaturePosition(cid), 10)
        end
    end
    return true
end

Would've been done in the first post if you would've provided the entire script, which you should do for future reference.
 
Solution
I honestly don't know where this case-sensitive thing came from, every script I see like this uses lower() on the creature name to check for it, which is useless because technically that'd create even more name conflicts (assuming using lower() is intended to reduce name conflicts).


Try this:
Lua:
function onCastSpell(cid, var)
    local target = getCreatureTarget(cid)
    if target > 0 then
        if isInArray(undeadCheck, getCreatureName(target)) then
            doSendMagicEffect(getCreaturePosition(cid), 10)
        end
    end
    return true
end

Would've been done in the first post if you would've provided the entire script, which you should do for future reference.

It still doesn't work... I'm trying quite a few things here but still couldn't get it to work.

I've made it work like this but it is far from the most efficient way, but as long as it works I guess. With arrays I could make codes similar to this one much easier to implement.

Lua:
target = getCreatureTarget(cid)
monster = getCreatureName(target)

if (monster == "Skeleton" or monster == "Skeleton Guardian" or monster == "Apparition" or monster == "Ghoul" or monster == "Slime" or monster == "Aberration" or monster == "Blood Skeleton" or monster == "Skeleton Bear")then
    doSendMagicEffect(getCreaturePosition(cid), 10)
end
 
Last edited:
Try this and show me what it prints (just for debugging purposes so we can know what's going wrong):
Lua:
function onCastSpell(cid, var)
    local target = getCreatureTarget(cid)
    print('target: ', target)
    if target > 0 then
        print('creature name: ', getCreatureName(target))
        if isInArray(undeadCheck, getCreatureName(target)) then
            doSendMagicEffect(getCreaturePosition(cid), 10)
        end
    end
    return true
end
 
Try this and show me what it prints (just for debugging purposes so we can know what's going wrong):
Lua:
function onCastSpell(cid, var)
    local target = getCreatureTarget(cid)
    print('target: ', target)
    if target > 0 then
        print('creature name: ', getCreatureName(target))
        if isInArray(undeadCheck, getCreatureName(target)) then
            doSendMagicEffect(getCreaturePosition(cid), 10)
        end
    end
    return true
end

Tried this, the console doesn't print anything when I cast the spell.
 
I have no ideas for you then, if you say it works with the magic effect then now it doesn't there's something else wrong other than the script and I couldn't tell you what it is.
 
I have no ideas for you then, if you say it works with the magic effect then now it doesn't there's something else wrong other than the script and I couldn't tell you what it is.

No problem, I will do it the hard way and it's alright.
Thanks for the time and effort you've put into this!
 
Back
Top Bottom