• 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!

[movement] Hopefully an easy fix

Gaddhuve

Member
Joined
May 6, 2011
Messages
76
Solutions
1
Reaction score
19
Hello,

Im learning lua and came a cross a line which I am not sure how it should be written correctly. Would appriciate if someone could take a look at it.

So basically you step on a tile and after x seconds an NPC will appear, Im writing it like this but it wont work.


Code:
addEvent(doCreateNPC, 3000, t.NPC, t.pos)

Thanks in advance!
 
Something like this?

<globalevent name="npc" interval="3600" event="script" value="npc.lua"/>

Code:
function onThink(interval, lastExecution, thinkInterval)
local pos = {x=1054,y=913,z=7}
local npc = doCreateNpc("dalk", pos )
local nMin = 20
addEvent(doRemoveCreature, nMin*60*1000, npc)
return true
end
 
Hi Limos!

Im using TFS 0.3.6, a bit old but works fine for me.

This is is script Im playing with:
Code:
local t = {
    npc = 'NPC',
    npc2 = 'NPC2',
    storage = 5000,
    pos = {x=500, y=500, z=7},
    pos2 = {x=500, y=501, z=7}
}

function onStepIn(cid, item, position, fromPosition)
    if isPlayer(cid) and getPlayerStorageValue(cid, t.storage) == -1 then
        setPlayerStorageValue(cid, t.storage, 1)

        local v = doCreateNpc(t.npc, t.pos)
   addEvent(doRemoveCreature, 4000, v)
   addEvent(doCreateNPC, 5000, t.npc2, t.pos2)
    end
end

I do realize that my
Code:
addEvent(doCreateNPC, 5000, t.npc2, t.pos2)
is probably wrong. Was thinking about adding a g to it:

Code:
        local g = addEvent(doCreateNpc, 5000t, npc2, t.pos2)
   addEvent(doRemoveCreature, 6000, g)

But wont work either haha. No bugs in colsole. What do you think?
 
Last edited:
Code:
local t = {
     npc = 'NPC',
     npc2 = 'NPC2',
     storage = 5000,
     pos = {x=500, y=500, z=7},
     pos2 = {x=500, y=501, z=7}
}

function onStepIn(cid, item, position, fromPosition)
     if isPlayer(cid) and getPlayerStorageValue(cid, t.storage) == -1 then
         setPlayerStorageValue(cid, t.storage, 1)

         doCreateNpc(t.npc, t.pos)
         addEvent(doRemoveCreature, 4000, getCreatureByName(t.npc))
         addEvent(doCreateNpc, 5000, t.npc2, t.pos2)
     end
     return true
end
 
Dude, thank you. I really do appriciate your help. Nice to know OTland has good folks like yourself. Cheers a bunch!
 
Code:
local function myFunctionRemove(v)
    return doRemoveCreature(v)
end

local function myFunction(name, pos)
    local npc = doCreateNpc(name, pos)
    return addEvent(myFunctionRemove, 5000, npc)
end

function onStepIn(cid, item, position, fromPosition)
    ...
    addEvent(myFunction, 5000, name, pos)
    ...
    return true
end
 
Last edited:
Let's say I wanted to go even further with this and make a small change so that NPC2 also will be removed after x seconds
Code:
local t = {
npc = 'NPC1',
npc2 = 'NPC2',
storage = 5000,
pos = {x=500, y=500, z=7},
pos2 = {x=500, y=501, z=7}
}

function onStepIn(cid, item, position, fromPosition)
if isPlayer(cid) and getPlayerStorageValue(cid, t.storage) == -1 then
setPlayerStorageValue(cid, t.storage, 1)

doCreateNpc(t.npc1, t.pos)
addEvent(doRemoveCreature, 4000, getCreatureByName(t.npc1))
addEvent(doRemoveCreature, 7000, getCreatureByName(t.npc2))
addEvent(doCreateNpc, 5000, t.npc2, t.pos2)

end
return true
end
Just added one of your lines but the problem with this is the getCreatureByName only seems to detect creatures that exists before the addEvent. Is there any way around this?
Getting the old classic "luaDoRemoveCreature(). Creature not found"

Much obliged ;)
 
Code:
local t = {
     npc = 'NPC',
     npc2 = 'NPC2',
     storage = 5000,
     pos = {x=500, y=500, z=7},
     pos2 = {x=500, y=501, z=7}
}

local function doRemoveNPC()
     return doRemoveCreature(getCreatureByName(t.npc2))
end

function onStepIn(cid, item, position, fromPosition)
     if isPlayer(cid) and getPlayerStorageValue(cid, t.storage) == -1 then
         setPlayerStorageValue(cid, t.storage, 1)

         doCreateNpc(t.npc, t.pos)
         addEvent(doRemoveCreature, 4000, getCreatureByName(t.npc))
         addEvent(doCreateNpc, 5000, t.npc2, t.pos2)
         addEvent(doRemoveNPC, 7000)
     end
     return true
end
If you add the parameters in addEvent, they will be loaded at the moment addEvent is loaded, but the function with those parameters will be executed after certain time.
So if you do it like this, it will do getCreatureByName(t.npc2) after 7 seconds.
 
If I may I would like to ask you one more question. After that you will never see me again haha.

The very last function I want in this script is to make NPC2 execute a doCreatureSay() before he is removed.

My first thought was to add a simple addEvent doCreatureSay

Code:
local t = {
npc = 'NPC',
npc2 = 'NPC2',
storage = 5000,
pos = {x=500, y=500, z=7},
pos2 = {x=500, y=501, z=7},
text = 'Hello world'
}

local function doRemoveNPC()
return doRemoveCreature(getCreatureByName(t.npc2))
end

function onStepIn(cid, item, position, fromPosition)
if isPlayer(cid) and getPlayerStorageValue(cid, t.storage) == -1 then
setPlayerStorageValue(cid, t.storage, 1)

doCreateNpc(t.npc, t.pos)
addEvent(doRemoveCreature, 4000, getCreatureByName(t.npc))
addEvent(doCreateNpc, 5000, t.npc2, t.pos2)
addEvent(doRemoveNPC, 7000)
addEvent(doCreatureSay, 6000, t.npc2, t.text, TALKTYPE_SAY, t.pos2)      
end
return true
end

... but after getting the same Creature not found() in colsole I realize that the issue probably is the same as you explained in your last post. So what I did was tried to add another local function just like you did

Code:
local function doSayNPC()
        return doCreatureSay(t.npc2, t.text, TALKTYPE_SAY, t.pos4)

end

... but with the same result. Any thoughts?
 
Code:
doCreatureSay(cid, text, type, pos)
cid is a value in numbers!! Why you didn't used my script?

Look at this:
Code:
local t = {
npc = 'NPC', -- here is just only the name of the NPC, ONLY THE NAME!
...

Remember: on the server can be lots of npcs with the same name, your server is storing a unique value for each of creature/npc/player on the server. To get that value you have to use functions like I wrote for you in my last post. Rest of scripts what others shown to you will not work cuz they are doing exactly the same bad thing.

If you want to make a script what is removing a npc what is already spawned on map - you have to locate that npc with function getThingFromPos. That function will return a value in numbers! Not a name of that npc.
For example, if your npc with name "Bob" is staying on position {pos.x = 100, pos.y = 100, pos.z = 7} then this function:
Code:
 getThingFromPos({pos.x = 100, pos.y = 100, pos.z = 7})
Will return something like this:
Code:
62031516537
If you want to make this npc disappear you can use one of those scripts:
Code:
doRemoveCreature(62031516537)
or
Code:
local npc = getThingFromPos({pos.x = 100, pos.y = 100, pos.z = 7})
if getCreatureName(npc) == "Bob" then
    doRemoveCreature(npc)
end
or this, script with addEvent thing:
Code:
local function removeThisBorringNpc(cid)
    return doRemoveCreature(cid)
end

function onStep()
    local npc = getThingFromPos({pos.x = 100, pos.y = 100, pos.z = 7})
    if getCreatureName(npc) == "Bob" then
        addEvent(removeThisBorringNpc, 5000, npc)
    end
    return true
end

---

If you don't belive in that cid is a value in numbers, and monsters/npcs/players are stored as a unique id on server. You can use this simple thing what will permanently change your thinking about scripts:
Code:
doCreatureSay(cid, cid, TALKTYPE_SAY)

---

If you learned that what I wrote up, you can go to lesson 2.
Function:
Code:
getCreatureByName(name)
Is returning a value in numbers again! Where numbers are correspond to a specific creature.
But you have to know one important thing. Every time that creature move, change look direction or it's health and mana changes - his unique id changing too!
So, if you used a function getCreatureByName(t.npc2) that will return a value example 100000001. And after 5 seconds when you want to remove that creature with new function what is once again getting a unique value - you will always get an error 'creature not found'. Becouse during those 5 seconds this npc's unique id changed ten times! Now that value is equal to 1000000045. What is different then your old value. This is why you are getting that error every time.

Lesson 3:
Correct reading order of the scripts:
2mpbcs2.jpg


---
I'll be glad if you subscribe my Youtube channel. Link is in my signature. :p
Here is a fully working script:
Code:
local t = {
    npc = 'NPC', -- this is just a name
    npc2 = 'NPC2', -- this is just a name
    storage = 5000,
    pos = {x=500, y=500, z=7},
    pos2 = {x=500, y=501, z=7},
    text = 'Hello world'
}

local function myFunctionRemove(v)
    return doRemoveCreature(v)
end

local function myFunction(name, pos, time)
    local npc = doCreateNpc(name, pos)
    if name = t.npc2 then
        addEvent(doCreatureSay, 1000, npc, t.text, 1)
    end
    return addEvent(myFunctionRemove, time, npc)
end

function onStepIn(cid, item, position, fromPosition)
    if isPlayer(cid) and getPlayerStorageValue(cid, t.storage) == -1 then
        setPlayerStorageValue(cid, t.storage, 1)
        addEvent(myFunction, 0, t.npc, t.pos, 4000)
        addEvent(myFunction, 5000, t.npc2, t.pos2, 2000)      
    end
    return true
end

And here a bit less complicated version:
Code:
local t = {
    npc = 'NPC', -- this is just a name
    npc2 = 'NPC2', -- this is just a name
    storage = 5000,
    pos = {x=500, y=500, z=7},
    pos2 = {x=500, y=501, z=7},
    text = 'Hello world'
}

local function myFunctionRemove(v)
    return doRemoveCreature(v)
end

local function myFunction(name, pos)
    local npc = doCreateNpc(name, pos)
    if name = t.npc then
        addEvent(myFunctionRemove, 4000, npc)
    elseif name = t.npc2 then
        addEvent(doCreatureSay, 1000, npc, t.text, 1)
        addEvent(myFunctionRemove, 2000, npc)
    end
    return true
end

function onStepIn(cid, item, position, fromPosition)
    if isPlayer(cid) and getPlayerStorageValue(cid, t.storage) == -1 then
        setPlayerStorageValue(cid, t.storage, 1) 
        addEvent(myFunction, 0, t.npc, t.pos)
        addEvent(myFunction, 5000, t.npc2, t.pos2)      
    end
    return true
end
 
Last edited:
Back
Top