• 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!
  • 2026 staff recruitment is open! Check it out and consider applying!

Lua NPC Random Talk affects all Npcs.

darkshin

New old member
Joined
Dec 14, 2010
Messages
231
Reaction score
22
Hey Guys, I was trying to make an NPC do a random talk with timing and animation.
I've made it work, but, somehow, all Npcs around my server started to talk the function like it were in their scripts aswell. Its pretty weird, I can't find out why.

Any ideas?

Code:
local function speech1(cid)
return selfSay("Upon the hearth the fire is red..")
end

local function soundEffect(cid)
return doSendMagicEffect(getCreaturePosition(getNpcCid()), 25)
end

local lastSound = 0


function onThink()

if lastSound < os.time() then
    lastSound = (os.time() + 10)
         if math.random(1, 100) < 7 then
           addEvent(speech1, 100)
           addEvent(soundEffect, 100)
end
end
 
The functions are local so I don't think it should. Any errors in the console?

Also, you don't need the cid parameter in the local functions
 
The functions are local so I don't think it should. Any errors in the console?

Also, you don't need the cid parameter in the local functions

No errors at the console, so I should just left it empty?


The first link isn't what I expect, but the second there's alot of things I could try out.
 
i just added the first script for you to learn how to use the voice module, also you can edit the last one for your own purposes
 
you may use a table defined in a lib file
npcLinesStor = {}

then in npc files
npcLinesStor[getNpcId()] = value

btw. cid is not passed in addEvent, onThink is an globalevent, not npc function
there is npcHandler:: onThink or something like that in npc file

if you want to manipulate npcs from external script you need to declare proper variable by defining it in npc script like:
npcToMove1 = getNpcId()
 
i just added the first script for you to learn how to use the voice module, also you can edit the last one for your own purposes
Okey, Thank you! I m trying to findout a simplest way, before trying yours. Because my Npc already have 1850 lines XD.

EDIT: I understood now that the VoiceModule is a lib. and the Towncryer example was calling it. Ayway, it does not work with TFS 1.1

you may use a table defined in a lib file
npcLinesStor = {}

then in npc files
npcLinesStor[getNpcId()] = value

btw. cid is not passed in addEvent, onThink is an globalevent, not npc function
there is npcHandler:: onThink or something like that in npc file

if you want to manipulate npcs from external script you need to declare proper variable by defining it in npc script like:
npcToMove1 = getNpcId()

Indeed, onThink showed itself as a globalevent for npcs. Also npcHandler::eek:nThink() is called inside of it, so I think that anything called inside the main onThink, will be a globalevent.

So I made a new function called "speech" with the function that was inside the main onThink and I called it outside any function as:

Code:
npcHandler:setCallback(CALLBACK_ONTHINK, speech)

It also showed to be a globalfunction. Also, the cids can be used, normally, in addEvent as an argument like "npc.uid". But I just can't make it work.

About the table inside a lib file, could you give me an example? I didn't follow.
 
Last edited:
Back
Top